import {
  AlertTriangle,
  CheckCircle2,
  Circle,
  Clock,
  Info,
  Lock,
  ShieldCheck,
  XCircle,
  type LucideIcon,
} from "lucide-react";
import { cn } from "@/lib/utils";

type Tone = "success" | "warning" | "danger" | "neutral" | "info";
const MAP: Record<string, [Tone, string, LucideIcon]> = {
  met: ["success", "Met", CheckCircle2],
  approved: ["success", "Approved", CheckCircle2],
  present: ["success", "Present", CheckCircle2],
  accepted: ["success", "Accepted", CheckCircle2],
  finalised: ["success", "Finalised", CheckCircle2],
  active: ["success", "Active", CheckCircle2],
  resolved: ["success", "Resolved", CheckCircle2],
  eligible: ["success", "Eligible", CheckCircle2],
  clear_pass: ["success", "Clear pass", ShieldCheck],
  yes: ["success", "Yes", CheckCircle2],
  at_risk: ["warning", "At risk", AlertTriangle],
  due_soon: ["warning", "Due soon", Clock],
  due: ["warning", "Due", Clock],
  pending_review: ["warning", "Pending review", Clock],
  returned: ["warning", "Returned", AlertTriangle],
  inconclusive: ["warning", "Inconclusive", AlertTriangle],
  under_review: ["warning", "Under review", Clock],
  reserve: ["warning", "Reserve", Circle],
  excused: ["warning", "Excused", Circle],
  escalated: ["warning", "Escalated", AlertTriangle],
  onboarding: ["warning", "Onboarding", Clock],
  reopened: ["warning", "Reopened", AlertTriangle],
  not_met: ["danger", "Not met", XCircle],
  overdue: ["danger", "Overdue", XCircle],
  rejected: ["danger", "Rejected", XCircle],
  absent: ["danger", "Absent", XCircle],
  failed: ["danger", "Failed", XCircle],
  clear_fail: ["danger", "Clear fail", XCircle],
  withdrawn: ["danger", "Withdrawn", XCircle],
  not_eligible: ["danger", "Not eligible", XCircle],
  suspended: ["danger", "Suspended", XCircle],
  no: ["neutral", "No", Circle],
  blocked: ["neutral", "Blocked", Lock],
  draft: ["neutral", "Draft", Circle],
  pending: ["neutral", "Pending", Circle],
  upcoming: ["neutral", "Upcoming", Circle],
  no_data: ["neutral", "No data", Circle],
  not_due: ["neutral", "Not yet due", Circle],
  planned: ["neutral", "Planned", Circle],
  closed: ["neutral", "Closed", Circle],
  superseded: ["neutral", "Superseded", Circle],
  not_applicable: ["neutral", "N/A", Circle],
  ended: ["neutral", "Ended", Circle],
  invited: ["info", "Invited", Info],
  disabled: ["neutral", "Disabled", Circle],
  info: ["info", "Info", Info],
  in_field: ["info", "In field", Info],
  submitted: ["info", "Submitted", Info],
  open: ["info", "Open", Info],
  new: ["info", "New", Info],
  ongoing: ["info", "Ongoing", Info],
  acknowledged: ["info", "Acknowledged", Info],
  in_progress: ["info", "In progress", Info],
  responded: ["info", "Responded", Info],
  drawn: ["info", "Drawn", Info],
};
const TONE: Record<Tone, string> = {
  success: "bg-success-soft text-success",
  warning: "bg-warning-soft text-warning",
  danger: "bg-danger-soft text-danger",
  neutral: "bg-surface-2 text-muted-foreground",
  info: "bg-info-soft text-info",
};
export function StatusBadge({
  status,
  label,
  size = "sm",
}: {
  status: string;
  label?: string;
  size?: "sm" | "md";
}) {
  const [tone, def, Icon] =
    MAP[status] ?? (["neutral", status.replace(/_/g, " "), Circle] as [Tone, string, LucideIcon]);
  return (
    <span
      className={cn(
        "inline-flex items-center gap-1 whitespace-nowrap rounded-full font-medium",
        TONE[tone],
        size === "sm" ? "px-2 py-0.5 text-[12px]" : "px-2.5 py-1 text-[13px]",
      )}
    >
      <Icon className="h-3.5 w-3.5 shrink-0" aria-hidden="true" />
      {label ?? def}
    </span>
  );
}
