// Package checklists holds the static checklist templates that users
// instantiate on /checklists/{slug}. Template data lives in Go (not in
// the DB) because it's curated content, versioned with the code, and
// rarely changes. Per-user state lives in paliad.checklist_instances.
//
// Both the HTTP layer (internal/handlers) and the service layer
// (internal/services) import this package — handlers serve the template
// JSON to the frontend, services validate slugs on POST and count items
// for progress responses without needing handler types.
package checklists

// Item is a single checklist point.
type Item struct {
	LabelDE string `json:"labelDE"`
	LabelEN string `json:"labelEN"`
	NoteDE  string `json:"noteDE,omitempty"`
	NoteEN  string `json:"noteEN,omitempty"`
	Rule    string `json:"rule,omitempty"`
}

// Group bundles related items under a heading.
type Group struct {
	TitleDE string `json:"titleDE"`
	TitleEN string `json:"titleEN"`
	Items   []Item `json:"items"`
}

// Template is the full definition of one checklist (UPC Klageschrift,
// EPA Einspruch, etc.). Slugs are URL-safe; client state keys are
// "g{groupIdx}-i{itemIdx}" (positional).
type Template struct {
	Slug          string  `json:"slug"`
	TitleDE       string  `json:"titleDE"`
	TitleEN       string  `json:"titleEN"`
	DescriptionDE string  `json:"descriptionDE"`
	DescriptionEN string  `json:"descriptionEN"`
	Regime        string  `json:"regime"` // UPC | DE | EPA
	CourtDE       string  `json:"courtDE"`
	CourtEN       string  `json:"courtEN"`
	DeadlineDE    string  `json:"deadlineDE,omitempty"`
	DeadlineEN    string  `json:"deadlineEN,omitempty"`
	ReferenceDE   string  `json:"referenceDE,omitempty"`
	ReferenceEN   string  `json:"referenceEN,omitempty"`
	Groups        []Group `json:"groups"`
}

// Summary is the shape returned by the list endpoint — template metadata
// without the full item tree.
type Summary struct {
	Slug          string `json:"slug"`
	TitleDE       string `json:"titleDE"`
	TitleEN       string `json:"titleEN"`
	DescriptionDE string `json:"descriptionDE"`
	DescriptionEN string `json:"descriptionEN"`
	Regime        string `json:"regime"`
	CourtDE       string `json:"courtDE"`
	CourtEN       string `json:"courtEN"`
	ItemCount     int    `json:"itemCount"`
}

// TotalItems counts the items across every group of a template.
func TotalItems(t Template) int {
	n := 0
	for _, g := range t.Groups {
		n += len(g.Items)
	}
	return n
}

// Find returns the template for a slug, or ok=false if none match.
func Find(slug string) (Template, bool) {
	for _, t := range Templates {
		if t.Slug == slug {
			return t, true
		}
	}
	return Template{}, false
}

// Summaries returns a summary for every registered template.
func Summaries() []Summary {
	out := make([]Summary, 0, len(Templates))
	for _, t := range Templates {
		out = append(out, Summary{
			Slug:          t.Slug,
			TitleDE:       t.TitleDE,
			TitleEN:       t.TitleEN,
			DescriptionDE: t.DescriptionDE,
			DescriptionEN: t.DescriptionEN,
			Regime:        t.Regime,
			CourtDE:       t.CourtDE,
			CourtEN:       t.CourtEN,
			ItemCount:     TotalItems(t),
		})
	}
	return out
}
