import type { BookingStatus } from "@/types/booking";

export interface StatusStyle {
  label: string;
  color: string;
}

export const BOOKING_STATUS_STYLES: Record<string, StatusStyle> = {
  DRAFT: {
    label: "Draft",
    color: "bg-slate-100 text-slate-700 border-slate-300",
  },
  SUBMITTED: {
    label: "Submitted",
    color: "bg-amber-50 text-amber-700 border-amber-200",
  },
  CHANGES_REQUESTED: {
    label: "Changes Requested",
    color: "bg-orange-50 text-orange-700 border-orange-200",
  },
  PENDING_APPROVAL: {
    label: "Pending Approval",
    color: "bg-amber-50 text-amber-700 border-amber-200",
  },
  APPROVED_PENDING_SIGNATURE: {
    label: "Pending Signature",
    color: "bg-sky-50 text-sky-700 border-sky-200",
  },
  APPROVED: {
    label: "Approved",
    color: "bg-[color:var(--freight-brand-muted)] text-[color:var(--freight-brand)] border-[color:var(--freight-brand-border)]",
  },
  CONTRACT_READY: {
    label: "Contract Ready",
    color: "bg-indigo-50 text-indigo-700 border-indigo-200",
  },
  SIGNED_CUSTOMER: {
    label: "Customer Signed",
    color: "bg-sky-50 text-sky-700 border-sky-200",
  },
  FULLY_EXECUTED: {
    label: "Fully Executed",
    color: "bg-indigo-50 text-indigo-700 border-indigo-200",
  },
  PNR_GENERATED: {
    label: "PNR Generated",
    color: "bg-violet-50 text-violet-700 border-violet-200",
  },
  PAYMENT_VERIFICATION_IN_PROGRESS: {
    label: "Payment Verification",
    color: "bg-amber-50 text-amber-800 border-amber-200",
  },
  SELECTED_FOR_BATCH: {
    label: "Selected for Batch",
    color: "bg-orange-50 text-orange-700 border-orange-200",
  },
  EXPIRED: {
    label: "Expired",
    color: "bg-red-50 text-red-700 border-red-200",
  },
  PAID: {
    label: "Paid",
    color: "bg-[color:var(--freight-brand-muted)] text-[color:var(--freight-brand)] border-[color:var(--freight-brand-border)]",
  },
  IN_TRANSIT: {
    label: "In Transit",
    color: "bg-sky-50 text-sky-700 border-sky-200",
  },
  COMPLETED: {
    label: "Completed",
    color: "bg-indigo-50 text-indigo-700 border-indigo-200",
  },
  REJECTED: {
    label: "Rejected",
    color: "bg-red-50 text-red-700 border-red-200",
  },
  CANCELLED: {
    label: "Cancelled",
    color: "bg-red-50 text-red-700 border-red-200",
  },
  PENDING_CONSOLIDATION: {
    label: "Pending Consolidation",
    color: "bg-amber-50 text-amber-700 border-amber-200",
  },
  CONSOLIDATED: {
    label: "Consolidated",
    color: "bg-indigo-50 text-indigo-700 border-indigo-200",
  },
  OPERATION_REQUEST_PENDING: {
    label: "Operation Review",
    color: "bg-amber-50 text-amber-700 border-amber-200",
  },
  OPERATION_CHANGES_REQUESTED: {
    label: "Operation Changes",
    color: "bg-orange-50 text-orange-700 border-orange-200",
  },
  OPERATION_PRICE_PENDING_CONFIRM: {
    label: "Price Confirm",
    color: "bg-amber-50 text-amber-700 border-amber-200",
  },
  ROAD_DISPATCH_PENDING: {
    label: "Truck Dispatch",
    color: "bg-blue-50 text-blue-700 border-blue-200",
  },
};

export interface StatusMeta {
  title: string;
  description: string;
  color: string;
  stage: number;
}

export const BOOKING_STATUS_META: Record<string, StatusMeta> = {
  DRAFT: {
    title: "Draft",
    description: "Booking is being prepared by the customer.",
    color: "text-slate-500",
    stage: 0,
  },
  SUBMITTED: {
    title: "Submitted",
    description: "Awaiting staff review.",
    color: "text-amber-600",
    stage: 0,
  },
  CHANGES_REQUESTED: {
    title: "Changes Requested",
    description: "Returned to customer for updates.",
    color: "text-orange-600",
    stage: 0,
  },
  PENDING_APPROVAL: {
    title: "Pending Approval",
    description: "Moving through internal approval chain.",
    color: "text-amber-600",
    stage: 1,
  },
  APPROVED_PENDING_SIGNATURE: {
    title: "Pending Signature",
    description: "Awaiting director or CEO signature steps.",
    color: "text-sky-600",
    stage: 1,
  },
  APPROVED: {
    title: "Approved",
    description: "Contract generated automatically; awaiting customer signature.",
    color: "text-[color:var(--freight-brand)]",
    stage: 2,
  },
  CONTRACT_READY: {
    title: "Contract Ready",
    description: "Contract generated; awaiting customer signature.",
    color: "text-indigo-600",
    stage: 2,
  },
  SIGNED_CUSTOMER: {
    title: "Customer Signed",
    description: "Awaiting contract execution.",
    color: "text-sky-600",
    stage: 2,
  },
  FULLY_EXECUTED: {
    title: "Fully Executed",
    description: "Contract locked; awaiting customer payment.",
    color: "text-indigo-600",
    stage: 3,
  },
  PNR_GENERATED: {
    title: "PNR Generated",
    description: "ETB payment reference issued.",
    color: "text-violet-600",
    stage: 3,
  },
  PAYMENT_VERIFICATION_IN_PROGRESS: {
    title: "Payment Verification",
    description: "USD payment proof under review.",
    color: "text-amber-700",
    stage: 3,
  },
  SELECTED_FOR_BATCH: {
    title: "Selected for Batch",
    description: "Picked from the batch pool — pay within the window to secure the slot.",
    color: "text-amber-600",
    stage: 3,
  },
  AWAITING_PAYMENT: {
    title: "Selected for Batch",
    description: "Picked from the batch pool — pay within the window to secure the slot.",
    color: "text-amber-600",
    stage: 3,
  },
  EXPIRED: {
    title: "Expired",
    description: "Pay window missed — move to another schedule or cancel.",
    color: "text-red-600",
    stage: 3,
  },
  PAID: {
    title: "Paid",
    description: "Payment confirmed; ready for operations.",
    color: "text-[color:var(--freight-brand)]",
    stage: 4,
  },
  IN_TRANSIT: {
    title: "In Transit",
    description: "Shipment is on the railway network.",
    color: "text-sky-600",
    stage: 4,
  },
  COMPLETED: {
    title: "Completed",
    description: "Booking fulfilled.",
    color: "text-indigo-600",
    stage: 5,
  },
  REJECTED: {
    title: "Rejected",
    description: "Booking was rejected.",
    color: "text-red-600",
    stage: -1,
  },
  CANCELLED: {
    title: "Cancelled",
    description: "Booking was cancelled.",
    color: "text-red-600",
    stage: -1,
  },
  PENDING_CONSOLIDATION: {
    title: "Pending Consolidation",
    description: "Blocked — waiting for a consolidation partner before approval.",
    color: "text-amber-600",
    stage: 0,
  },
  CONSOLIDATED: {
    title: "Consolidated",
    description: "Paired with another booking.",
    color: "text-indigo-600",
    stage: 0,
  },
};

export const BOOKING_LIST_TABS = [
  { key: "all", label: "All bookings", statuses: null as string[] | null },
  {
    key: "intake",
    label: "Submitted",
    statuses: ["SUBMITTED", "PENDING_CONSOLIDATION"],
  },
  {
    key: "in_approval",
    label: "In approval",
    statuses: ["PENDING_APPROVAL", "APPROVED_PENDING_SIGNATURE"],
  },
  {
    key: "approved_contract",
    label: "Approved & contract",
    statuses: [
      "APPROVED",
      "CONTRACT_READY",
      "SIGNED_CUSTOMER",
      "FULLY_EXECUTED",
    ],
  },
  {
    key: "payment",
    label: "Payment",
    statuses: [
      "FULLY_EXECUTED",
      "PNR_GENERATED",
      "PAYMENT_VERIFICATION_IN_PROGRESS",
      "SELECTED_FOR_BATCH",
      "EXPIRED",
    ],
  },
  {
    key: "ops_review",
    label: "Ops review",
    statuses: [
      "OPERATION_REQUEST_PENDING",
      "OPERATION_CHANGES_REQUESTED",
      "OPERATION_PRICE_PENDING_CONFIRM",
    ],
  },
  {
    key: "operations",
    label: "Operations",
    statuses: ["PAID", "IN_TRANSIT", "ROAD_DISPATCH_PENDING"],
  },
  { key: "completed", label: "Completed", statuses: ["COMPLETED"] },
  { key: "closed", label: "Closed", statuses: ["REJECTED", "CANCELLED"] },
] as const;

export type BookingStatusTabKey = (typeof BOOKING_LIST_TABS)[number]["key"];

export const WORKFLOW_STAGES = [
  {
    label: "Submission",
    statuses: [
      "DRAFT",
      "SUBMITTED",
      "CHANGES_REQUESTED",
      "PENDING_CONSOLIDATION",
    ],
  },
  {
    label: "Approval",
    statuses: ["PENDING_APPROVAL", "APPROVED_PENDING_SIGNATURE"],
  },
  {
    label: "Contract",
    statuses: ["APPROVED", "CONTRACT_READY", "SIGNED_CUSTOMER", "FULLY_EXECUTED"],
  },
  {
    label: "Payment",
    statuses: [
      "FULLY_EXECUTED",
      "PNR_GENERATED",
      "PAYMENT_VERIFICATION_IN_PROGRESS",
      "SELECTED_FOR_BATCH",
      "EXPIRED",
    ],
  },
  {
    label: "Operations",
    statuses: ["PAID", "IN_TRANSIT"],
  },
  { label: "Done", statuses: ["COMPLETED"] },
] as const;

export function getStatusMeta(status: BookingStatus | string): StatusMeta {
  return (
    BOOKING_STATUS_META[status] ?? {
      title: status,
      description: "",
      color: "text-muted-foreground",
      stage: 0,
    }
  );
}

export function getWorkflowStageIndex(status: BookingStatus | string): number {
  const meta = getStatusMeta(status);
  if (meta.stage < 0) return -1;
  return meta.stage;
}
