// Order Status System — central definitions
export const SALE_STATUSES = [
  "pending",
  "confirmed",
  "processing",
  "ready_for_delivery",
  "completed",
  "delivered",
  "partial_delivered",
  "returned",
  "cancelled",
] as const;

export type SaleStatus = (typeof SALE_STATUSES)[number];

export const STATUS_LABELS: Record<SaleStatus, { en: string; bn: string }> = {
  pending: { en: "Pending", bn: "অপেক্ষমাণ" },
  confirmed: { en: "Confirmed", bn: "নিশ্চিত" },
  processing: { en: "Processing", bn: "প্রক্রিয়াধীন" },
  ready_for_delivery: { en: "Ready for Delivery", bn: "ডেলিভারির জন্য প্রস্তুত" },
  completed: { en: "Completed", bn: "সম্পন্ন" },
  delivered: { en: "Delivered", bn: "ডেলিভার্ড" },
  partial_delivered: { en: "Partial Delivered", bn: "আংশিক ডেলিভার্ড" },
  returned: { en: "Returned", bn: "ফেরত" },
  cancelled: { en: "Cancelled", bn: "বাতিল" },
};

export const STATUS_COLORS: Record<SaleStatus, string> = {
  pending: "bg-yellow-500/15 text-yellow-700 dark:text-yellow-400 border-yellow-500/30",
  confirmed: "bg-blue-500/15 text-blue-700 dark:text-blue-400 border-blue-500/30",
  processing: "bg-indigo-500/15 text-indigo-700 dark:text-indigo-400 border-indigo-500/30",
  ready_for_delivery: "bg-cyan-500/15 text-cyan-700 dark:text-cyan-400 border-cyan-500/30",
  completed: "bg-green-500/15 text-green-700 dark:text-green-400 border-green-500/30",
  delivered: "bg-emerald-500/15 text-emerald-700 dark:text-emerald-400 border-emerald-500/30",
  partial_delivered: "bg-teal-500/15 text-teal-700 dark:text-teal-400 border-teal-500/30",
  returned: "bg-orange-500/15 text-orange-700 dark:text-orange-400 border-orange-500/30",
  cancelled: "bg-red-500/15 text-red-700 dark:text-red-400 border-red-500/30",
};

const TRANSITIONS: Record<SaleStatus, SaleStatus[]> = {
  pending: ["confirmed", "cancelled"],
  confirmed: ["processing", "cancelled", "pending"],
  processing: ["ready_for_delivery", "completed", "cancelled"],
  ready_for_delivery: ["delivered", "partial_delivered", "cancelled"],
  completed: ["delivered", "partial_delivered", "returned"],
  partial_delivered: ["delivered", "returned"],
  delivered: ["returned"],
  returned: [],
  cancelled: [],
};

export function nextStatuses(current: SaleStatus): SaleStatus[] {
  return TRANSITIONS[current] ?? [];
}

export function statusLabel(s: string, isBn: boolean): string {
  const k = s as SaleStatus;
  return STATUS_LABELS[k] ? STATUS_LABELS[k][isBn ? "bn" : "en"] : s;
}

// =====================================================
// Sale Status (Payment / Refund state) — separate from order workflow
// =====================================================
export const SALE_PAYMENT_STATUSES = [
  "draft",
  "pending_payment",
  "partially_paid",
  "paid",
  "refunded",
  "returned",
] as const;

export type SalePaymentStatus = (typeof SALE_PAYMENT_STATUSES)[number];

export const SALE_PAYMENT_LABELS: Record<SalePaymentStatus, { en: string; bn: string }> = {
  draft: { en: "Draft", bn: "খসড়া" },
  pending_payment: { en: "Pending Payment", bn: "পেমেন্ট বাকি" },
  partially_paid: { en: "Partially Paid", bn: "আংশিক পরিশোধ" },
  paid: { en: "Paid", bn: "পরিশোধিত" },
  refunded: { en: "Refunded", bn: "ফেরত দেওয়া" },
  returned: { en: "Returned", bn: "ফেরত" },
};

export const SALE_PAYMENT_COLORS: Record<SalePaymentStatus, string> = {
  draft: "bg-slate-500/15 text-slate-700 dark:text-slate-400 border-slate-500/30",
  pending_payment: "bg-red-500/15 text-red-700 dark:text-red-400 border-red-500/30",
  partially_paid: "bg-yellow-500/15 text-yellow-700 dark:text-yellow-400 border-yellow-500/30",
  paid: "bg-green-500/15 text-green-700 dark:text-green-400 border-green-500/30",
  refunded: "bg-purple-500/15 text-purple-700 dark:text-purple-400 border-purple-500/30",
  returned: "bg-orange-500/15 text-orange-700 dark:text-orange-400 border-orange-500/30",
};

export function salePaymentLabel(s: string, isBn: boolean): string {
  const k = s as SalePaymentStatus;
  return SALE_PAYMENT_LABELS[k] ? SALE_PAYMENT_LABELS[k][isBn ? "bn" : "en"] : s;
}
