package agent

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	"reasonix/internal/event"
)

// AskTool lets the model put a structured multiple-choice question (or a few) to
// the user mid-task and get the answer back — for genuine forks the model can't
// resolve from the request or the code (which library, which approach, …) rather
// than guessing or asking in prose. The frontend renders selectable options, the
// user picks, and the choices come back as the tool result. It reaches the user
// through the Asker carried on the call
// context (CallContext); with no asker (headless runs) it returns an explicit
// model-assumption fallback so an autonomous run never blocks or pretends a user
// answered.
type AskTool struct{}

func NewAskTool() *AskTool { return &AskTool{} }

func (*AskTool) Name() string { return "ask" }

func (*AskTool) Description() string {
	return "Ask the user one or more multiple-choice questions when you hit a decision that is genuinely theirs to make — one you can't resolve from the request, the code, or sensible defaults. The frontend shows the options for the user to pick; their choices are returned to you. Prefer this over asking in prose for any real fork (which approach, which library, scope). Don't use it for decisions with an obvious default — pick the sensible option and proceed. Tool-approval modes such as YOLO do not answer these questions for the user. Each question has a short `header` (a tab label), the `question` text, 2-4 `options` (each a `label` and optional `description`; put any recommended option first), and `multiSelect` when more than one may apply."
}

func (*AskTool) Schema() json.RawMessage {
	return json.RawMessage(`{
"type":"object",
"properties":{
  "questions":{
    "type":"array",
    "minItems":1,
    "maxItems":3,
    "description":"1-3 related questions to ask together. Same ambiguity is asked only once.",
    "items":{
      "type":"object",
      "properties":{
        "header":{"type":"string","description":"Very short label for the question (a tab title), e.g. \"Library\"."},
        "question":{"type":"string","description":"The full question to ask."},
        "options":{
          "type":"array","minItems":2,"maxItems":4,
          "description":"The choices. Put any recommended option first.",
          "items":{
            "type":"object",
            "properties":{
              "label":{"type":"string","description":"The choice text (concise)."},
              "description":{"type":"string","description":"Optional one-line explanation of the choice."}
            },
            "required":["label"]
          }
        },
        "multiSelect":{"type":"boolean","description":"Allow selecting more than one option."}
      },
      "required":["question","header","options"]
    }
  },
  "decision_id":{"type":"string","description":"Required when reopening a previously accepted decision; cite the original decision_id."},
  "new_evidence":{"type":"string","description":"Required with decision_id when asking again after the user already accepted a consequence."}
},
"required":["questions"]
}`)
}

// ReadOnly is true: asking has no host side effects, so it never needs approval
// and stays available in plan mode (clarifying scope while planning is fine).
func (*AskTool) ReadOnly() bool { return true }

func (*AskTool) Execute(ctx context.Context, args json.RawMessage) (string, error) {
	p, err := parseAskArgs(args)
	if err != nil {
		return "", err
	}

	qs := make([]event.AskQuestion, 0, len(p.Questions))
	for i, q := range p.Questions {
		question := strings.TrimSpace(q.Question)
		if question == "" || len(q.Options) < 2 {
			return "", fmt.Errorf("question %d: a question and at least two options are required", i+1)
		}
		opts := make([]event.AskOption, len(q.Options))
		seenLabels := make(map[string]int, len(q.Options))
		for j, o := range q.Options {
			label := strings.TrimSpace(o.Label)
			if label == "" {
				return "", fmt.Errorf("question %d option %d: label is required", i+1, j+1)
			}
			if prev, ok := seenLabels[label]; ok {
				return "", fmt.Errorf("question %d option %d: duplicate label %q also used by option %d", i+1, j+1, label, prev+1)
			}
			seenLabels[label] = j
			opts[j] = event.AskOption{Label: label, Description: strings.TrimSpace(o.Description)}
		}
		qs = append(qs, event.AskQuestion{
			ID:      fmt.Sprintf("q%d", i+1),
			Header:  strings.TrimSpace(q.Header),
			Prompt:  question,
			Options: opts,
			Multi:   q.MultiSelect,
		})
	}

	id := strings.TrimSpace(p.DecisionID)
	explicitID := id != ""
	if id == "" {
		id = decisionIDForQuestions(qs)
	}
	if dec, ok := existingDecision(ctx, id); ok {
		if strings.TrimSpace(p.Evidence) == "" {
			return fmt.Sprintf("Host reused accepted decision %s. The user already chose: %s. Continue with that decision unless you supply decision_id and new_evidence.", dec.ID, dec.Answer), nil
		}
	} else if !explicitID {
		if dec, matched := matchingExistingDecision(ctx, qs); matched {
			return fmt.Sprintf("Host reused accepted decision %s for the same ambiguity. The user already chose: %s. Continue with that decision; to reopen it, cite decision_id %s and supply new_evidence.", dec.ID, dec.Answer, dec.ID), nil
		}
	} else if explicitID {
		if _, hasAcceptedDecision := firstExistingDecision(ctx); hasAcceptedDecision {
			return "", fmt.Errorf("unknown decision_id %q; cite the original accepted decision_id and include new_evidence to reopen it", id)
		}
	}

	_, _, asker, ok := CallContext(ctx)
	if !ok || asker == nil {
		// Headless / no interactive user: don't block an autonomous run, but make
		// the provenance explicit so the model doesn't treat this as a user choice.
		return "No interactive user answered. This is a model-assumption fallback, not a user answer. Proceed with your best judgment, state the assumption you made, and prefer the safest reversible option when choices differ in risk.", nil
	}

	answers, err := asker.Ask(ctx, qs)
	if err != nil {
		return "", fmt.Errorf("ask: %w", err)
	}
	summary := formatAnswers(qs, answers)
	rememberDecisionForQuestions(ctx, id, qs[0].Prompt, summary, qs)
	return summary + "\n\ndecision_id: " + id, nil
}

// formatAnswers renders the user's selections as a compact, model-facing summary,
// keyed by question header so the model can tell which answer is which. When the
// user picked nothing at all (the "just chat" / dismiss path), it returns an
// explicit stop signal instead of a per-question "(no answer)" — otherwise the
// model reads the empty result as license to proceed and acts unasked.
func formatAnswers(qs []event.AskQuestion, answers []event.AskAnswer) string {
	pick := make(map[string][]string, len(answers))
	for _, a := range answers {
		pick[a.QuestionID] = a.Selected
	}
	answered := 0
	for _, q := range qs {
		if len(pick[q.ID]) > 0 {
			answered++
		}
	}
	if answered == 0 {
		return "The user dismissed the question without choosing — read this as \"don't decide for me, let's just talk.\" Do not pick an option, run a tool, or take any further action toward this; stop and wait for the user's next message."
	}
	var b strings.Builder
	b.WriteString("The user answered:\n")
	for _, q := range qs {
		sel := pick[q.ID]
		label := q.Header
		if label == "" {
			label = q.Prompt
		}
		if len(sel) == 0 {
			fmt.Fprintf(&b, "- %s: (left unanswered — don't assume a choice)\n", label)
			continue
		}
		fmt.Fprintf(&b, "- %s: %s\n", label, strings.Join(sel, ", "))
	}
	return strings.TrimRight(b.String(), "\n")
}
