import {
  ArrowRight,
  CalendarClock,
  CheckCircle2,
  Clock3,
  FileCheck2,
  FilePen,
  FileUp,
  MapPin,
  ShieldCheck,
  Truck,
  Wallet,
  type LucideIcon,
} from "lucide-react";
import { Freight } from "@edr/types";

export const cv = (token: string) => {
  const [name, shade] = token.split(".");
  return `var(--mantine-color-${name}-${shade ?? "6"})`;
};

export const formatPct = (n: number) => `${n >= 0 ? "+" : ""}${n}%`;

export const ACTIVE_STATUSES = [
  "DRAFT",
  "SUBMITTED",
  "PENDING_APPROVAL",
  "IN_TRANSIT",
  "ARRIVED",
];

export interface StageConfig {
  stage: number;
  icon: LucideIcon;
  iconColor: string;
  tile: string;
  hint: string;
  step: string;
  badgeLabel: string;
  badgeBg: string;
  badgeText: string;
  badgeDot: string;
  action: {
    label: string;
    kind: "dark" | "amber" | "outline";
    icon?: LucideIcon;
  };
}

export const STATUS_CONFIG: Record<string, StageConfig> = {
  DRAFT: {
    stage: 0,
    icon: FilePen,
    iconColor: "edr-slate",
    tile: "edr-slate-soft",
    hint: "Draft saved · not yet submitted",
    step: "edr-step",
    badgeLabel: "Draft",
    badgeBg: "edr-slate-soft",
    badgeText: "edr-slate",
    badgeDot: "edr-step",
    action: { label: "Continue", kind: "dark" },
  },
  SUBMITTED: {
    stage: 1,
    icon: FileCheck2,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Quote being prepared by EDR",
    step: "edr-blue-dot",
    badgeLabel: "Reviewing",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "View", kind: "outline" },
  },
  CHANGES_REQUESTED: {
    stage: 1,
    icon: FilePen,
    iconColor: "edr-amber-text",
    tile: "edr-amber-soft",
    hint: "Changes requested · please update",
    step: "edr-accent",
    badgeLabel: "Revise",
    badgeBg: "edr-amber-soft",
    badgeText: "edr-amber-text",
    badgeDot: "edr-accent",
    action: { label: "Update", kind: "dark" },
  },
  PENDING_APPROVAL: {
    stage: 2,
    icon: FileCheck2,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Pending internal approval",
    step: "edr-blue-dot",
    badgeLabel: "Pending",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "View", kind: "outline" },
  },
  APPROVED_PENDING_SIGNATURE: {
    stage: 2,
    icon: FileCheck2,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Approved · awaiting signature",
    step: "edr-blue-dot",
    badgeLabel: "For Signature",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "Review", kind: "outline" },
  },
  APPROVED: {
    stage: 2,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Quote approved · ready to sign",
    step: "edr-green.5",
    badgeLabel: "Approved",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  CONTRACT_READY: {
    stage: 2,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Contract ready · awaiting signature",
    step: "edr-green.5",
    badgeLabel: "Contract Ready",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "Review", kind: "outline" },
  },
  SIGNED_CUSTOMER: {
    stage: 3,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Signed by customer · internal processing",
    step: "edr-green.5",
    badgeLabel: "Signed",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  FULLY_EXECUTED: {
    stage: 3,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Fully executed · generating PNR",
    step: "edr-green.5",
    badgeLabel: "Executed",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  AWAITING_DOCUMENTS: {
    stage: 3,
    icon: FileUp,
    iconColor: "edr-amber-text",
    tile: "edr-amber-soft",
    hint: "Clearance documents needed",
    step: "edr-accent",
    badgeLabel: "Upload documents",
    badgeBg: "edr-amber-soft",
    badgeText: "edr-amber-text",
    badgeDot: "edr-accent",
    action: { label: "Upload documents", kind: "amber", icon: ArrowRight },
  },
  DOCUMENTS_UNDER_REVIEW: {
    stage: 3,
    icon: ShieldCheck,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Clearance under review · re-upload any queried docs",
    step: "edr-blue-dot",
    badgeLabel: "In review",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "Review documents", kind: "outline" },
  },
  CLEARANCE_READY: {
    stage: 3,
    icon: CalendarClock,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Cleared · choose a shipment day to proceed",
    step: "edr-green.5",
    badgeLabel: "Cleared",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "Schedule & proceed", kind: "amber", icon: ArrowRight },
  },
  OPERATION_REQUEST_PENDING: {
    stage: 3,
    icon: FileCheck2,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Operation request under review by operations",
    step: "edr-blue-dot",
    badgeLabel: "Op. review",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "View", kind: "outline" },
  },
  OPERATION_REQUESTED: {
    stage: 3,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Operation requested · operator taking it forward",
    step: "edr-green.5",
    badgeLabel: "Operation requested",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  OPERATION_CHANGES_REQUESTED: {
    stage: 3,
    icon: FilePen,
    iconColor: "edr-amber-text",
    tile: "edr-amber-soft",
    hint: "Operations requested changes · please review",
    step: "edr-accent",
    badgeLabel: "Revise",
    badgeBg: "edr-amber-soft",
    badgeText: "edr-amber-text",
    badgeDot: "edr-accent",
    action: { label: "Review", kind: "dark" },
  },
  OPERATION_PRICE_PENDING_CONFIRM: {
    stage: 3,
    icon: Wallet,
    iconColor: "edr-amber-text",
    tile: "edr-amber-soft",
    hint: "Price adjusted · confirm to proceed",
    step: "edr-accent",
    badgeLabel: "Confirm price",
    badgeBg: "edr-amber-soft",
    badgeText: "edr-amber-text",
    badgeDot: "edr-accent",
    action: { label: "Confirm", kind: "amber", icon: ArrowRight },
  },
  ROAD_DISPATCH_PENDING: {
    stage: 3,
    icon: Truck,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Accepted · awaiting truck dispatch",
    step: "edr-blue-dot",
    badgeLabel: "Awaiting dispatch",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "View", kind: "outline" },
  },
  PNR_GENERATED: {
    stage: 3,
    icon: FileCheck2,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "PNR generated · awaiting payment verification",
    step: "edr-blue-dot",
    badgeLabel: "PNR Ready",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "View", kind: "outline" },
  },
  PAYMENT_VERIFICATION_IN_PROGRESS: {
    stage: 2,
    icon: Clock3,
    iconColor: "edr-amber-text",
    tile: "edr-amber-soft",
    hint: "Verifying payment · please wait",
    step: "edr-accent",
    badgeLabel: "Verifying",
    badgeBg: "edr-amber-soft",
    badgeText: "edr-amber-text",
    badgeDot: "edr-accent",
    action: { label: "View", kind: "outline" },
  },
  SELECTED_FOR_BATCH: {
    stage: 2,
    icon: Wallet,
    iconColor: "edr-amber-text",
    tile: "edr-amber-soft",
    hint: "Selected for batch · payment due within 1 hour",
    step: "edr-accent",
    badgeLabel: "Pay Now",
    badgeBg: "edr-amber-soft",
    badgeText: "edr-amber-text",
    badgeDot: "edr-accent",
    action: { label: "Pay now", kind: "amber", icon: ArrowRight },
  },
  EXPIRED: {
    stage: 1,
    icon: Clock3,
    iconColor: "edr-red",
    tile: "edr-red-soft",
    hint: "Payment window expired · contact support",
    step: "edr-red",
    badgeLabel: "Expired",
    badgeBg: "edr-red-soft",
    badgeText: "edr-red",
    badgeDot: "edr-red",
    action: { label: "Contact", kind: "outline" },
  },
  PAID: {
    stage: 3,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Payment received · awaiting dispatch",
    step: "edr-green.5",
    badgeLabel: "Paid",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  TRUCK_ASSIGNED: {
    stage: 3,
    icon: Truck,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Truck assigned · preparing for pickup",
    step: "edr-green.5",
    badgeLabel: "Truck assigned",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  IN_TRANSIT: {
    stage: 3,
    icon: Truck,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "In transit · on schedule",
    step: "edr-green.5",
    badgeLabel: "In Transit",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "Track", kind: "outline", icon: MapPin },
  },
  ARRIVED: {
    stage: 3,
    icon: MapPin,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Arrived at destination yard · awaiting release",
    step: "edr-green.5",
    badgeLabel: "Arrived",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "Track", kind: "outline", icon: MapPin },
  },
  COMPLETED: {
    stage: 4,
    icon: CheckCircle2,
    iconColor: "edr-slate",
    tile: "edr-slate-soft2",
    hint: "Completed · awaiting delivery",
    step: "edr-green.5",
    badgeLabel: "Completed",
    badgeBg: "edr-slate-soft2",
    badgeText: "edr-slate",
    badgeDot: "edr-step",
    action: { label: "View", kind: "outline" },
  },
  DELIVERED: {
    stage: 4,
    icon: CheckCircle2,
    iconColor: "edr-slate",
    tile: "edr-slate-soft2",
    hint: "Delivered · POD ready",
    step: "edr-green.5",
    badgeLabel: "Delivered",
    badgeBg: "edr-slate-soft2",
    badgeText: "edr-slate",
    badgeDot: "edr-step",
    action: { label: "View POD", kind: "outline" },
  },
  CANCELLED: {
    stage: 0,
    icon: FilePen,
    iconColor: "edr-red",
    tile: "edr-red-soft",
    hint: "Cancelled",
    step: "edr-red",
    badgeLabel: "Cancelled",
    badgeBg: "edr-red-soft",
    badgeText: "edr-red",
    badgeDot: "edr-red",
    action: { label: "View", kind: "outline" },
  },
  REJECTED: {
    stage: 0,
    icon: FilePen,
    iconColor: "edr-red",
    tile: "edr-red-soft",
    hint: "Rejected · contact support",
    step: "edr-red",
    badgeLabel: "Rejected",
    badgeBg: "edr-red-soft",
    badgeText: "edr-red",
    badgeDot: "edr-red",
    action: { label: "Contact", kind: "outline" },
  },
  PENDING_CONSOLIDATION: {
    stage: 3,
    icon: Clock3,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Awaiting consolidation",
    step: "edr-blue-dot",
    badgeLabel: "Consolidating",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "View", kind: "outline" },
  },
  CONSOLIDATED: {
    stage: 3,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Consolidated · ready for dispatch",
    step: "edr-green.5",
    badgeLabel: "Consolidated",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  PRICE_CHANGED_PENDING_CONFIRM: {
    stage: 1,
    icon: Wallet,
    iconColor: "edr-amber-text",
    tile: "edr-amber-soft",
    hint: "Price changed · confirm to continue",
    step: "edr-accent",
    badgeLabel: "Confirm price",
    badgeBg: "edr-amber-soft",
    badgeText: "edr-amber-text",
    badgeDot: "edr-accent",
    action: { label: "Confirm", kind: "amber", icon: ArrowRight },
  },
  READY_FOR_ASSIGNMENT: {
    stage: 2,
    icon: FileCheck2,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Approved · awaiting wagon assignment",
    step: "edr-blue-dot",
    badgeLabel: "Assigning",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "View", kind: "outline" },
  },
  WAGON_ASSIGNED: {
    stage: 3,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Wagon assigned · preparing for loading",
    step: "edr-green.5",
    badgeLabel: "Wagon assigned",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  INVOICED: {
    stage: 3,
    icon: Wallet,
    iconColor: "edr-blue",
    tile: "edr-blue-soft",
    hint: "Invoice issued · awaiting payment",
    step: "edr-blue-dot",
    badgeLabel: "Invoiced",
    badgeBg: "edr-blue-soft",
    badgeText: "edr-blue",
    badgeDot: "edr-blue-dot",
    action: { label: "View", kind: "outline" },
  },
  CONTRACT_ACTIVE: {
    stage: 3,
    icon: CheckCircle2,
    iconColor: "edr-green.7",
    tile: "edr-soft",
    hint: "Contract active · accepting orders",
    step: "edr-green.5",
    badgeLabel: "Active",
    badgeBg: "edr-soft",
    badgeText: "edr-green.7",
    badgeDot: "edr-green.5",
    action: { label: "View", kind: "outline" },
  },
  CONTRACT_CLOSED: {
    stage: 4,
    icon: CheckCircle2,
    iconColor: "edr-slate",
    tile: "edr-slate-soft2",
    hint: "Contract closed · quantity used or window elapsed",
    step: "edr-step",
    badgeLabel: "Closed",
    badgeBg: "edr-slate-soft2",
    badgeText: "edr-slate",
    badgeDot: "edr-step",
    action: { label: "View", kind: "outline" },
  },
};

export const ACTION_PROPS: Record<
  string,
  { bg: string; c: string; bd?: string }
> = {
  dark: { bg: "edr-ink", c: "white" },
  amber: { bg: "edr-accent", c: "white" },
  outline: { bg: "edr-card", c: "edr-text", bd: "1px solid edr-border" },
};

export const INVOICE_BADGE: Record<
  Freight.InvoiceStatus,
  { label: string; bg: string; text: string }
> = {
  [Freight.InvoiceStatus.Draft]: { label: "Draft", bg: "edr-slate-soft", text: "edr-slate" },
  [Freight.InvoiceStatus.Issued]: { label: "Due soon", bg: "edr-amber-soft", text: "edr-amber-text" },
  [Freight.InvoiceStatus.Pending]: { label: "Due soon", bg: "edr-amber-soft", text: "edr-amber-text" },
  [Freight.InvoiceStatus.PartiallyPaid]: { label: "Partially paid", bg: "edr-amber-soft", text: "edr-amber-text" },
  [Freight.InvoiceStatus.Paid]: { label: "Paid", bg: "edr-soft", text: "edr-green.7" },
  [Freight.InvoiceStatus.Overdue]: { label: "Overdue", bg: "edr-red-soft", text: "edr-red" },
  [Freight.InvoiceStatus.Cancelled]: { label: "Cancelled", bg: "edr-slate-soft", text: "edr-slate" },
  [Freight.InvoiceStatus.Refunded]: { label: "Refunded", bg: "edr-blue-soft", text: "edr-blue" },
  [Freight.InvoiceStatus.Expired]: { label: "Expired", bg: "edr-red-soft", text: "edr-red" },
};
