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

export const supplierShipmentWorkflow: WorkflowDefinition = {
  steps: [
    { status: "draft", label: { en: "Draft", ar: "مسودة" }, tone: "neutral" },
    { status: "ready", label: { en: "Ready", ar: "جاهز" }, tone: "pending" },
    { status: "dispatched", label: { en: "Dispatched", ar: "تم الإرسال" }, tone: "processing" },
    { status: "in_transit", label: { en: "In Transit", ar: "قيد النقل" }, tone: "processing" },
    { status: "arrived", label: { en: "Arrived", ar: "وصل" }, tone: "info" },
    { status: "linked_to_receiving", label: { en: "Linked To Receiving", ar: "مرتبط بالاستلام" }, tone: "warning" },
    { status: "closed", label: { en: "Closed", ar: "مغلق" }, tone: "success" },
  ],
  actions: {
    draft: [
      { key: "confirm_dispatch", label: { en: "Confirm Dispatch", ar: "تأكيد الإرسال" }, variant: "primary" },
      { key: "cancel", label: { en: "Cancel", ar: "إلغاء" }, variant: "danger", danger: true, requireReason: true },
    ],
    ready: [
      { key: "confirm_dispatch", label: { en: "Confirm Dispatch", ar: "تأكيد الإرسال" }, variant: "primary" },
      { key: "cancel", label: { en: "Cancel", ar: "إلغاء" }, variant: "danger", danger: true, requireReason: true },
    ],
    dispatched: [
      { key: "mark_arrived", label: { en: "Mark Arrived", ar: "تأكيد الوصول" }, variant: "primary" },
      { key: "cancel", label: { en: "Cancel", ar: "إلغاء" }, variant: "danger", danger: true, requireReason: true },
    ],
    in_transit: [
      { key: "mark_arrived", label: { en: "Mark Arrived", ar: "تأكيد الوصول" }, variant: "primary" },
    ],
    arrived: [
      { key: "link_to_receiving", label: { en: "Transfer to Receiving", ar: "ترحيل إلى الاستلام" }, variant: "primary" },
    ],
    linked_to_receiving: [],
  },
  stepIndex: (status: string) => {
    const steps = ["draft", "ready", "dispatched", "in_transit", "arrived", "linked_to_receiving", "closed"]
    return steps.indexOf(status)
  },
}

export const supplierShipmentPermissionMap: Record<string, string> = {
  confirm_dispatch: "warehouse:delivery_execute",
  mark_arrived: "warehouse:delivery_execute",
  cancel: "warehouse:delivery_create",
  close: "warehouse:delivery_close",
  link_to_receiving: "warehouse:delivery_execute",
}

export const supplierShipmentStatusTone: Record<SupplierShipmentStatus, "neutral" | "pending" | "processing" | "info" | "warning" | "success" | "danger"> = {
  draft: "neutral",
  ready: "pending",
  dispatched: "processing",
  in_transit: "processing",
  arrived: "info",
  linked_to_receiving: "warning",
  closed: "success",
  cancelled: "danger",
}

export const supplierShipmentStatusLabel: Record<SupplierShipmentStatus, { en: string; ar: string }> = {
  draft: { en: "Draft", ar: "مسودة" },
  ready: { en: "Ready", ar: "جاهز" },
  dispatched: { en: "Dispatched", ar: "تم الإرسال" },
  in_transit: { en: "In Transit", ar: "قيد النقل" },
  arrived: { en: "Arrived", ar: "وصل" },
  linked_to_receiving: { en: "Linked To Receiving", ar: "مرتبط بالاستلام" },
  closed: { en: "Closed", ar: "مغلق" },
  cancelled: { en: "Cancelled", ar: "ملغي" },
}
