import { Badge } from "@/components/ui/badge";
import {
  FilePlus2, Upload, Loader2, FileCheck, Target,
  Lightbulb, Compass, PenLine, SearchCheck, FileText,
  FileOutput, CheckCircle2, Award, ShieldCheck, BookOpen,
  Microscope, PlayCircle,
} from "lucide-react";
import type { CaseStatus } from "@/lib/api/cases";
import type { LucideIcon } from "lucide-react";

const STATUS_LABELS: Record<CaseStatus, string> = {
  new: "חדש",
  uploading: "מעלה",
  processing: "בעיבוד",
  documents_ready: "מסמכים מוכנים",
  analyst_verified: "ניתוח אומת",
  research_complete: "מחקר הושלם",
  outcome_set: "תוצאה נקבעה",
  brainstorming: "סיעור מוחות",
  direction_approved: "כיוון אושר",
  analysis_enriched: "ניתוח הועמק",
  ready_for_writing: "מוכן לכתיבה",
  drafting: "בכתיבה",
  qa_review: "בדיקת איכות",
  drafted: "טיוטה",
  exported: "יוצא",
  reviewed: "נבדק",
  final: "סופי",
};

const STATUS_ICONS: Record<CaseStatus, LucideIcon> = {
  new:                FilePlus2,
  uploading:          Upload,
  processing:         Loader2,
  documents_ready:    FileCheck,
  analyst_verified:   ShieldCheck,
  research_complete:  BookOpen,
  outcome_set:        Target,
  brainstorming:      Lightbulb,
  direction_approved: Compass,
  analysis_enriched:  Microscope,
  ready_for_writing:  PlayCircle,
  drafting:           PenLine,
  qa_review:          SearchCheck,
  drafted:            FileText,
  exported:           FileOutput,
  reviewed:           CheckCircle2,
  final:              Award,
};

const STATUS_DESCRIPTIONS: Record<CaseStatus, string> = {
  new:                "התיק נוצר וממתין להעלאת מסמכים",
  uploading:          "מסמכים בתהליך העלאה לשרת",
  processing:         "המערכת מעבדת ומנתחת את המסמכים",
  documents_ready:    "כל המסמכים עובדו ומוכנים לעבודה",
  analyst_verified:   "ניתוח ראשוני אומת — ממתין למחקר תקדימים",
  research_complete:  "מחקר תקדימים הושלם — ממתין לבחירת תוצאה",
  outcome_set:        "נקבעה תוצאה צפויה לערר",
  brainstorming:      "ניתוח כיוונים אפשריים להחלטה",
  direction_approved: "כיוון ההחלטה אושר — בהעמקת ניתוח",
  analysis_enriched:  "ניתוח הועמק ופסיקה אומתה — מוכן לכתיבה",
  ready_for_writing:  "הכל מוכן — ממתין לכותב ההחלטה",
  drafting:           "טיוטת ההחלטה בתהליך כתיבה",
  qa_review:          "הטיוטה בבדיקת איכות אוטומטית",
  drafted:            "טיוטה מוכנה לעיון",
  exported:           "ההחלטה יוצאה לקובץ DOCX",
  reviewed:           "ההחלטה נבדקה ע\"י היו\"ר",
  final:              "החלטה סופית — מוכנה להגשה",
};

/* Status color groups:
 * intake  → new, uploading, processing          (muted parchment)
 * prep    → documents_ready, outcome_set        (info blue)
 * thinking→ brainstorming, direction_approved   (gold)
 * writing → drafting, qa_review, drafted        (warn amber)
 * done    → exported, reviewed, final           (success green) */
const STATUS_TONE: Record<CaseStatus, string> = {
  new:              "bg-rule-soft text-ink-muted border-rule",
  uploading:        "bg-rule-soft text-ink-muted border-rule",
  processing:       "bg-info-bg text-info border-info/30",
  documents_ready:  "bg-info-bg text-info border-info/40",
  analyst_verified: "bg-info-bg text-info border-info/40",
  research_complete:"bg-info-bg text-info border-info/40",
  outcome_set:      "bg-info-bg text-info border-info/40",
  brainstorming:    "bg-gold-wash text-gold-deep border-gold/40",
  direction_approved:"bg-gold-wash text-gold-deep border-gold/50",
  analysis_enriched:"bg-gold-wash text-gold-deep border-gold/50",
  ready_for_writing:"bg-gold-wash text-gold-deep border-gold/50",
  drafting:         "bg-warn-bg text-warn border-warn/40",
  qa_review:        "bg-warn-bg text-warn border-warn/40",
  drafted:          "bg-warn-bg text-warn border-warn/50",
  exported:         "bg-success-bg text-success border-success/40",
  reviewed:         "bg-success-bg text-success border-success/50",
  final:            "bg-success-bg text-success border-success/60 font-semibold",
};

export function StatusBadge({ status }: { status: CaseStatus }) {
  const Icon = STATUS_ICONS[status];
  return (
    <Badge
      variant="outline"
      className={`rounded-full px-2.5 py-0.5 text-[0.72rem] font-medium inline-flex items-center gap-1 ${STATUS_TONE[status] ?? ""}`}
    >
      {Icon && <Icon className="w-3 h-3 shrink-0" />}
      {STATUS_LABELS[status] ?? status}
    </Badge>
  );
}

export { STATUS_LABELS, STATUS_ICONS, STATUS_DESCRIPTIONS, STATUS_TONE };
