import { type LucideIcon, ShieldCheck, AlertTriangle, TrendingUp, TrendingDown, Sparkles, Wallet, PiggyBank, Repeat } from "lucide-react";

export type ChipTone = "success" | "warning" | "electric" | "purple" | "neutral";
export type Chip = { label: string; tone: ChipTone; icon?: LucideIcon };

const toneStyle: Record<ChipTone, string> = {
  success: "text-success border-success/30 bg-success/10",
  warning: "text-warning border-warning/30 bg-warning/10",
  electric: "text-electric border-electric/30 bg-electric/10",
  purple: "text-purple border-purple/30 bg-purple/10",
  neutral: "text-muted-foreground border-border bg-white/5",
};

export function StatusChip({ chip }: { chip: Chip }) {
  const Icon = chip.icon;
  return (
    <span className={`inline-flex items-center gap-1.5 rounded-full border px-2.5 py-1 text-[11px] font-medium ${toneStyle[chip.tone]}`}>
      {Icon && <Icon className="size-3" />} {chip.label}
    </span>
  );
}

export function StatusChipRow({ chips }: { chips: Chip[] }) {
  if (!chips.length) return null;
  return (
    <div className="flex gap-1.5 flex-wrap">
      {chips.map((c, i) => <StatusChip key={i} chip={c} />)}
    </div>
  );
}

/** Compute chips deterministically from user state (no LLM needed for these). */
export function deriveChips(input: {
  savingsMonths?: number;
  net?: number;
  earned?: number;
  spent?: number;
  budgetOveragePct?: number;
  topOverBudgetCategory?: string | null;
  recurringGrowthPct?: number;
  goalOnTrack?: boolean;
}): Chip[] {
  const out: Chip[] = [];
  if ((input.savingsMonths ?? 0) >= 3) out.push({ label: "Emergency fund healthy", tone: "success", icon: ShieldCheck });
  else if ((input.savingsMonths ?? 0) < 1) out.push({ label: "Emergency fund low", tone: "warning", icon: AlertTriangle });

  if ((input.net ?? 0) > 0) out.push({ label: "Cash flow positive", tone: "success", icon: TrendingUp });
  else if ((input.net ?? 0) < 0) out.push({ label: "Cash flow negative", tone: "warning", icon: TrendingDown });

  if ((input.budgetOveragePct ?? 0) > 90 && input.topOverBudgetCategory) {
    out.push({ label: `${input.topOverBudgetCategory} watch`, tone: "warning", icon: Wallet });
  }
  if ((input.recurringGrowthPct ?? 0) > 15) out.push({ label: "Subscriptions rising", tone: "purple", icon: Repeat });
  if (input.goalOnTrack) out.push({ label: "Goals on track", tone: "electric", icon: Sparkles });
  if (input.earned && input.spent && input.spent / input.earned < 0.6) out.push({ label: "Savings on track", tone: "success", icon: PiggyBank });
  return out.slice(0, 5);
}
