//go:build kendemo

// Command ken-demo-postgres is a downloadable, self-contained ken-mcp
// server for the PostgreSQL source tree. The pre-built search index and
// the Model2Vec embedding model are baked into the binary via //go:embed,
// so it starts instantly (loads the index in ~1-2s, no walk/chunk/embed)
// and needs no corpus checkout, no ~/.ken/model, and no network.
//
// Build (the kendemo tag keeps this out of the ken module's `go build
// ./...` / CI, and gates the //go:embed of the large assets):
//
//	# index.bin + model/ must be present (see demos/README — generated by
//	# `ken build-index <postgres> -o demos/postgres/index.bin --chunker=treesitter`
//	# and copying ~/.ken/model). Both are gitignored.
//	CGO_ENABLED=0 go build -tags=kendemo -o ken-demo-postgres ./demos/postgres
//
// Register in an MCP client (no env, no repo arg — the corpus is baked):
//
//	{ "mcpServers": { "ken-demo-postgres": {
//	    "command": "/path/to/ken-demo-postgres" } } }
//
// chunker=treesitter (real C AST boundaries; ken's regex chunker has no
// C ruleset). As of ADR-032 the treesitter chunker is a public package
// (chunk/treesitter), so this demo could live in its own module — it
// stays in-tree alongside ken/demos/kubernetes for a single paired
// launch, not out of necessity.
package main

import (
	"context"
	"embed"
	"io/fs"
	"log"

	"github.com/townsendmerino/ken/mcp"

	// Register the treesitter chunker so mcp.Run's ChunkerName validation
	// accepts "treesitter" and the pre-built index's chunker matches.
	// (regex + line come transitively via internal/search; treesitter is
	// opt-in — now a public package per ADR-032, importable from any module.)
	_ "github.com/townsendmerino/aikit/chunk/treesitter"
)

//go:embed index.bin
var indexBytes []byte

//go:embed model
var modelEmbed embed.FS

func main() {
	modelFS, err := fs.Sub(modelEmbed, "model")
	if err != nil {
		log.Fatalf("ken-demo-postgres: model fs.Sub: %v", err)
	}
	// fsys is unused on the pre-built-load happy path (PrebuiltIndex is
	// set); pass the model FS as a harmless non-nil placeholder. If the
	// embedded index ever fails to load, mcp.Run would try to live-index
	// fsys — there's no corpus here, so that degrades to an empty index;
	// the release verification protocol catches a bad embed before ship.
	if err := mcp.Run(context.Background(), modelFS, mcp.Options{
		Mode:          "hybrid",
		ChunkerName:   "treesitter",
		PrebuiltIndex: indexBytes,
		ModelFS:       modelFS,
		LogLevel:      "info", // so the "loaded pre-built index" line is visible
	}); err != nil {
		log.Fatalf("ken-demo-postgres: %v", err)
	}
}
