package tui

import (
	"image/color"

	"charm.land/lipgloss/v2"
)

// -- Theme palette --
//
// Generic color names mapped to a specific theme. To switch themes,
// update the hex values below — no other files need to change.
//
// Current theme: Catppuccin Frappé
// Reference: https://catppuccin.com/palette

var (
	themeRosewater = lipgloss.Color("#f2d5cf")
	themeFlamingo  = lipgloss.Color("#eebebe")
	themePink      = lipgloss.Color("#f4b8e4")
	themeMauve     = lipgloss.Color("#ca9ee6")
	themeRed       = lipgloss.Color("#e78284")
	themePeach     = lipgloss.Color("#ef9f76")
	themeYellow    = lipgloss.Color("#e5c890")
	themeGreen     = lipgloss.Color("#a6d189")
	themeTeal      = lipgloss.Color("#81c8be")
	themeSapphire  = lipgloss.Color("#85c1dc")
	themeBlue      = lipgloss.Color("#8caaee")
	themeLavender  = lipgloss.Color("#babbf1")
	themeText      = lipgloss.Color("#c6d0f5")
	themeSubtext0  = lipgloss.Color("#a5adce")
	themeOverlay2  = lipgloss.Color("#949cbb")
	themeOverlay1  = lipgloss.Color("#838ba7")
	themeOverlay0  = lipgloss.Color("#737994")
	themeSurface2  = lipgloss.Color("#626880")
	themeSurface1  = lipgloss.Color("#51576d")
	themeBase      = lipgloss.Color("#303446")
	themeMantle    = lipgloss.Color("#292c3c")
	themeCrust     = lipgloss.Color("#232634")
)

// -- Styles --

var (
	borderStyle = lipgloss.NewStyle().
			Border(lipgloss.RoundedBorder()).
			BorderForeground(themeOverlay0)

	titleStyle = lipgloss.NewStyle().
			Bold(true).
			Foreground(themeSapphire)

	selectedStyle = lipgloss.NewStyle().
			Bold(true).
			Background(themeSurface1).
			Foreground(themeText)

	permissionColor = themePeach
	questionColor   = themeYellow
	errorColor      = themeRed
	runningColor    = themeBlue
	idlePromptColor = themeOverlay1
	doneColor       = themeGreen
	prColor         = themeMauve
	mergedColor     = themeTeal
	planColor       = themeMauve
	effortColor     = themePink
	textInputColor  = themeYellow

	helpStyle      = lipgloss.NewStyle().Foreground(themeOverlay1)
	boldStyle      = lipgloss.NewStyle().Bold(true)
	costStyle      = lipgloss.NewStyle().Foreground(themePeach).Bold(true)
	planLabelStyle = lipgloss.NewStyle().Foreground(themeMauve).Bold(true)

	diffCollapsedStyle  = lipgloss.NewStyle().Foreground(themeOverlay1).Italic(true)
	diffScrollHintStyle = lipgloss.NewStyle().Foreground(themeSapphire)
)

type stateIcon struct {
	icon  string
	color color.Color
}

var stateIcons = map[string]stateIcon{
	"permission":  {"⚿", permissionColor},
	"question":    {"?", questionColor},
	"error":       {"✗", errorColor},
	"running":     {"▶", runningColor},
	"idle_prompt": {"○", idlePromptColor},
	"done":        {"✓", doneColor},
	"pr":          {"↑", prColor},
	"merged":      {"⏏", mergedColor},
	"plan":        {"☐", planColor},
}

var groupHeaders = map[int]struct {
	label string
	color color.Color
}{
	1: {"BLOCKED", permissionColor},
	2: {"WAITING", questionColor},
	3: {"RUNNING", runningColor},
	4: {"REVIEW", doneColor},
	5: {"PR", prColor},
	6: {"MERGED", mergedColor},
}

// isBlocked returns true when the agent needs a mechanical action (y/n) to continue.
func isBlocked(state string) bool {
	return state == "permission" || state == "plan"
}

// isWaiting returns true when the agent is stuck and needs user input or investigation.
func isWaiting(state string) bool {
	switch state {
	case "question", "error":
		return true
	}
	return false
}

// isReview returns true when the agent has completed its turn and needs review.
func isReview(state string) bool {
	switch state {
	case "done", "idle_prompt":
		return true
	}
	return false
}

// isPR returns true when the agent has a PR open and is waiting on GitHub.
func isPR(state string) bool {
	return state == "pr"
}

// isMerged returns true when the agent's branch has been merged and is safe to close.
func isMerged(state string) bool {
	return state == "merged"
}

// stateLabel returns a human-readable label for the agent state.
var stateLabels = map[string]string{
	"permission":  "Waiting for approval",
	"question":    "Asked a question",
	"error":       "Error",
	"running":     "Running",
	"idle_prompt": "Idle at prompt",
	"pr":          "PR open",
	"merged":      "Branch merged",
	"done":        "Done",
}
