package iace

import (
	"fmt"
	"strings"
)

// RenderProposalQueue turns judged dedup proposals into the human-review queue
// (markdown). Deterministic. Nothing here applies a change — every entry is a
// suggestion for a human to confirm, edit, commit, and pin with a GT case.
func RenderProposalQueue(machine string, proposals []JudgedProposal) string {
	var b strings.Builder
	fmt.Fprintf(&b, "# Dedup proposal queue — %s\n\n", machine)
	fmt.Fprintf(&b, "%d candidate(s) survived the deterministic GT wall. Propose-only — nothing is applied automatically.\n\n", len(proposals))

	for i, p := range proposals {
		c := p.Candidate
		fmt.Fprintf(&b, "## %d. keep %s  ⊃  drop %s   [%s → %s (%s)]\n",
			i+1, c.KeepPattern, c.DropPattern, p.Judge, p.Verdict, p.Confidence)
		fmt.Fprintf(&b, "- category %s · score %.2f (measures %.0f%%, zone %.0f%%, scenario %.0f%%)\n",
			c.Category, c.Score, c.MeasureJaccard*100, c.ZoneJaccard*100, c.ScenarioJaccard*100)
		fmt.Fprintf(&b, "- GT recall %.1f%% → %.1f%% when %s is dropped (wall: %s)\n",
			p.Screen.RecallBefore*100, p.Screen.RecallAfter*100, c.DropPattern, wallNote(p.Screen))
		fmt.Fprintf(&b, "- keep: %s\n- drop: %s\n", c.KeepHazardName, c.DropName)
		fmt.Fprintf(&b, "- judge rationale: %s\n", p.Rationale)
		fmt.Fprintf(&b, "- suggested action: %s\n\n", suggestedAction(p))
	}
	return b.String()
}

func wallNote(s ScreenResult) string {
	if s.DistinctGT {
		return fmt.Sprintf("distinct GT %s vs %s", s.KeepGT, s.DropGT)
	}
	return "recall-safe"
}

func suggestedAction(p JudgedProposal) string {
	switch p.Verdict {
	case VerdictDuplicate:
		return fmt.Sprintf("add %s to a supersession set, then a human confirms + commits + pins a GT case", p.Candidate.DropPattern)
	case VerdictDistinct:
		return "keep both — judge considers them distinct hazards"
	default:
		return "needs human (or higher-confidence LLM) review — no automatic action"
	}
}
