import type { WorkflowDefinition } from "@/components/workflow/workflow-config"
import type { PurchaseOrderStatus } from "@/types"

export const purchaseOrderWorkflow: WorkflowDefinition = {
  steps: [
    { status: "draft", label: { en: "Draft", ar: "مسودة" }, tone: "neutral" },
    { status: "pending_review", label: { en: "Pending Review", ar: "بانتظار المراجعة" }, tone: "pending" },
    { status: "pending_approval", label: { en: "Pending Approval", ar: "بانتظار الاعتماد" }, tone: "warning" },
    { status: "approved", label: { en: "Approved", ar: "معتمد" }, tone: "info" },
    { status: "sent_to_supplier", label: { en: "Sent To Supplier", ar: "مرسل للمورد" }, tone: "processing" },
    { status: "partially_supplied", label: { en: "Partially Supplied", ar: "توريد جزئي" }, tone: "processing" },
    { status: "fully_supplied", label: { en: "Fully Supplied", ar: "توريد كامل" }, tone: "success" },
    { status: "closed", label: { en: "Closed", ar: "مغلق" }, tone: "success" },
  ],
  actions: {
    draft: [
      { key: "submit_review", label: { en: "Submit for Review", ar: "إرسال للمراجعة" }, variant: "primary" },
      { key: "cancel", label: { en: "Cancel", ar: "إلغاء" }, variant: "danger", danger: true, requireReason: true },
    ],
    pending_review: [
      { key: "submit_approval", label: { en: "Submit for Approval", ar: "إرسال للاعتماد" }, variant: "primary" },
    ],
    pending_approval: [
      { key: "approve", label: { en: "Approve", ar: "اعتماد" }, variant: "primary" },
    ],
    approved: [
      { key: "send_to_supplier", label: { en: "Send to Supplier", ar: "إرسال للمورد" }, variant: "primary" },
      { key: "record_advance_payment", label: { en: "Record Advance Payment", ar: "تسجيل دفعة مقدمة" }, variant: "outline" },
    ],
    sent_to_supplier: [
      { key: "create_shipment", label: { en: "Create Supplier Shipment", ar: "إنشاء شحنة مورد" }, variant: "primary" },
      { key: "record_advance_payment", label: { en: "Record Advance Payment", ar: "تسجيل دفعة مقدمة" }, variant: "outline" },
    ],
    partially_supplied: [
      { key: "create_shipment", label: { en: "Create Supplier Shipment", ar: "إنشاء شحنة مورد" }, variant: "primary" },
    ],
    fully_supplied: [
      { key: "close", label: { en: "Close", ar: "إغلاق" }, variant: "primary" },
    ],
  },
  stepIndex: (status: string) => {
    const steps = ["draft", "pending_review", "pending_approval", "approved", "sent_to_supplier", "partially_supplied", "fully_supplied", "closed"]
    return steps.indexOf(status)
  },
}

export const purchaseOrderPermissionMap: Record<string, string> = {
  submit_review: "buy_orders:update",
  submit_approval: "buy_orders:review",
  approve: "buy_orders:approve",
  send_to_supplier: "buy_orders:update",
  create_shipment: "warehouse:delivery_create",
  record_advance_payment: "buy_orders:update",
  cancel: "buy_orders:cancel",
  close: "buy_orders:approve",
}

export const purchaseOrderStatusTone: Record<PurchaseOrderStatus, "neutral" | "pending" | "warning" | "info" | "processing" | "success" | "danger"> = {
  draft: "neutral",
  pending_review: "pending",
  pending_approval: "warning",
  approved: "info",
  sent_to_supplier: "processing",
  partially_supplied: "processing",
  fully_supplied: "success",
  closed: "success",
  cancelled: "neutral",
  rejected: "danger",
}

export const purchaseOrderStatusLabel: Record<PurchaseOrderStatus, { en: string; ar: string }> = {
  draft: { en: "Draft", ar: "مسودة" },
  pending_review: { en: "Pending Review", ar: "بانتظار المراجعة" },
  pending_approval: { en: "Pending Approval", ar: "بانتظار الاعتماد" },
  approved: { en: "Approved", ar: "معتمد" },
  sent_to_supplier: { en: "Sent To Supplier", ar: "مرسل للمورد" },
  partially_supplied: { en: "Partially Supplied", ar: "توريد جزئي" },
  fully_supplied: { en: "Fully Supplied", ar: "توريد كامل" },
  closed: { en: "Closed", ar: "مغلق" },
  cancelled: { en: "Cancelled", ar: "ملغي" },
  rejected: { en: "Rejected", ar: "مرفوض" },
}
