package exec

import "strings"

var commonPromptActions = [...]struct {
	domain string
	name   string
}{
	{domain: "task", name: "create"},
	{domain: "task", name: "claim"},
	{domain: "task", name: "checkpoint"},
	{domain: "task", name: "update"},
	{domain: "review", name: "add"},
}

// PromptAction is a typed projection of one built-in action used by the
// compiler-owned runtime guidance template. ExampleJSON remains catalog
// payload; the template labels and surrounds it.
type PromptAction struct {
	Domain      string
	Name        string
	ExampleJSON string
}

// PromptDomains returns the deterministic built-in action domains. The caller
// supplies these typed facts to the prompt compiler; it does not receive prose.
func (c *Catalog) PromptDomains() []string {
	if c == nil {
		return nil
	}
	var domains []string
	last := ""
	for _, action := range c.actionsSnapshot() {
		if action.Domain == last {
			continue
		}
		last = action.Domain
		domains = append(domains, action.Domain)
	}
	return domains
}

// PromptCommonActions returns the canonical typed examples kept inline in
// runtime guidance. All wording and punctuation remain in the registered
// template rather than this catalog projection.
func (c *Catalog) PromptCommonActions() []PromptAction {
	if c == nil {
		return nil
	}
	out := make([]PromptAction, 0, len(commonPromptActions))
	for _, ref := range commonPromptActions {
		action, ok := c.lookup(ref.domain, ref.name)
		if !ok || len(action.Examples) == 0 {
			continue
		}
		out = append(out, PromptAction{Domain: action.Domain, Name: action.Name, ExampleJSON: compactJSON(action.Examples[0].Input)})
	}
	return out
}

// RenderActionSummary retains the historical compact discovery projection for
// compatibility. Runtime prompt compilation uses PromptDomains and
// PromptCommonActions so semantic wording is owned by the registered template.
func (c *Catalog) RenderActionSummary() string {
	var b strings.Builder
	b.WriteString("AgentTeam Exec actions are discoverable on demand:\n")
	b.WriteString("- `agentteam exec --help` lists every action.\n")
	b.WriteString("- `agentteam exec <domain> --help` lists actions in one domain.\n")
	b.WriteString("- `agentteam exec <domain> <action> --help` shows one example; `--input-schema` / `--output-schema` show exact fields.\n")
	b.WriteString("Domains:")
	for _, domain := range c.PromptDomains() {
		b.WriteString(" `" + domain + "`")
	}
	b.WriteString(".\n")
	var common strings.Builder
	for _, action := range c.PromptCommonActions() {
		common.WriteString("- `agentteam exec " + action.Domain + " " + action.Name + " '" + action.ExampleJSON + "'`\n")
	}
	if common.Len() > 0 {
		b.WriteString("Common forms (replace values; no help needed):\n")
		b.WriteString(common.String())
	}
	return b.String()
}
