import {
  ArrowRight,
  CheckCircle2,
  Clock3,
  FileCheck2,
  FilePen,
  MapPin,
  Truck,
  Wallet,
  type LucideIcon,
} from "lucide-react";
import type { InvoiceStatus } from "@/pages/billing/invoices.mock";

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",
];

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" },
  },
  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" },
  },
  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 },
  },
  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" },
  },
};

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<
  InvoiceStatus,
  { label: string; bg: string; text: string }
> = {
  Draft: { label: "Draft", bg: "edr-slate-soft", text: "edr-slate" },
  Sent: { label: "Due soon", bg: "edr-amber-soft", text: "edr-amber-text" },
  Paid: { label: "Paid", bg: "edr-soft", text: "edr-green.7" },
  Overdue: { label: "Overdue", bg: "edr-red-soft", text: "edr-red" },
  Cancelled: { label: "Cancelled", bg: "edr-slate-soft", text: "edr-slate" },
};
