// Quickstart: the smallest program that gets an LLM responding through the
// Tetrate Agent Router (TARS) Go SDK. You bring an API key; the SDK lists the
// catalog and uses the first available model. Override with
// AGENTROUTER_QUICKSTART_MODEL. Customer and project are derived from the API
// key's session -- nothing to pass.
//
// Prerequisites (set in the environment):
//
//	AGENTROUTER_API_KEY    a long-lived key (looks like <keyid>.<secret>) for an existing project
//	AGENTROUTER_BASE_URL   optional; defaults to https://api.tetrate.ai
//
// The prompt itself is in the code below -- edit it to ask your own question.
// Then, from this directory:
//
//	go run .
//
// In a repo checkout: cd sdks/examples/go/quickstart && go run .
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	agentrouter "github.com/tetrateio/agentrouter-go"
)

func main() {
	ctx := context.Background()

	c, err := agentrouter.New(ctx, agentrouter.WithAPIKeyFromStandardEnv())
	if err != nil {
		log.Fatal(err)
	}

	// Pick the model. The inference handler requires a non-empty model, so when
	// AGENTROUTER_QUICKSTART_MODEL is unset we list the catalog and use the
	// first model's bare name (e.g. claude-haiku-4-5), which resolves its
	// provider server-side.
	model := os.Getenv("AGENTROUTER_QUICKSTART_MODEL")
	if model == "" {
		resp, err := c.Catalog().ListModels(ctx, "")
		if err != nil {
			log.Fatal(err)
		}
		models := resp.GetModels()
		if len(models) == 0 {
			log.Fatal("no models in catalog -- ask an admin to import a catalog bundle")
		}
		model = models[0].GetName()
	}

	// Edit this prompt to ask your own question.
	prompt := "Happy happy joy joy"

	// Customer and project default from the session backing the API key; pass
	// "" to let the server resolve them.
	res, err := c.Inference().Prompt(ctx, "", "", prompt, agentrouter.WithModel(model))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("model:      %s\n", res.Model)
	fmt.Printf("completion: %s\n", res.Completion)
	fmt.Printf("tokens:     %d prompt / %d completion\n", res.PromptTokens, res.CompletionTokens)
}
