import type { WorkflowDefinition } from "@/components/workflow/workflow-config"
import type { OutboundOrderStatus, PurchaseReturnStatus, PlatformAllocationStatus } from "@/types"

export const outboundOrderWorkflow: WorkflowDefinition = {
  steps: [
    { status: "draft", label: { en: "Create", ar: "إنشاء" }, tone: "neutral" },
    { status: "approved", label: { en: "Approve", ar: "اعتماد" }, tone: "info" },
    { status: "picking", label: { en: "Picking", ar: "التجهيز" }, tone: "pending" },
    { status: "dispatched", label: { en: "Dispatch / Handover", ar: "إرسال" }, tone: "processing" },
    { status: "delivered", label: { en: "Proof Uploaded", ar: "إثبات" }, tone: "warning" },
    { status: "closed", label: { en: "Close", ar: "إغلاق" }, tone: "success" },
  ],
  actions: {
    draft: [{ key: "approve", label: { en: "Approve", ar: "اعتماد" }, variant: "primary" }],
    approved: [{ key: "pick", label: { en: "Pick", ar: "تجهيز" }, variant: "primary" }],
    picking: [{ key: "dispatch", label: { en: "Dispatch", ar: "إرسال" }, variant: "primary" }],
    dispatched: [{ key: "confirm_handover", label: { en: "Confirm Handover + Upload Proof", ar: "تأكيد + إثبات" }, variant: "primary" }],
    delivered: [{ key: "close", label: { en: "Close", ar: "إغلاق" }, variant: "primary" }],
  },
  stepIndex: (status: string) => ["draft", "approved", "picking", "dispatched", "delivered", "closed"].indexOf(status),
}

export const outboundOrderStatusTone: Record<string, any> = {
  draft: "neutral", approved: "info", picking: "pending", dispatched: "processing", delivered: "warning", closed: "success", cancelled: "danger",
}

export const outboundOrderStatusLabel: Record<string, { en: string; ar: string }> = {
  draft: { en: "Draft", ar: "مسودة" },
  approved: { en: "Approved", ar: "معتمد" },
  picking: { en: "Picking", ar: "تجهيز" },
  dispatched: { en: "Dispatched", ar: "مرسل" },
  delivered: { en: "Delivered", ar: "تم التسليم" },
  closed: { en: "Closed", ar: "مغلق" },
  cancelled: { en: "Cancelled", ar: "ملغي" },
}

export const outboundOrderPermissionMap: Record<string, string> = {
  approve: "warehouse:delivery_approve",
  pick: "warehouse:delivery_execute",
  dispatch: "warehouse:delivery_execute",
  confirm_handover: "warehouse:delivery_execute",
  close: "warehouse:delivery_close",
}

export const purchaseReturnWorkflow: WorkflowDefinition = {
  steps: [
    { status: "draft", label: { en: "Create Return", ar: "إنشاء" }, tone: "neutral" },
    { status: "approved", label: { en: "Approve", ar: "اعتماد" }, tone: "info" },
    { status: "prepare_dispatch", label: { en: "Prepare Dispatch", ar: "تجهيز" }, tone: "pending" },
    { status: "posted", label: { en: "Outbound Posted", ar: "ترحيل" }, tone: "processing" },
    { status: "closed", label: { en: "Closed", ar: "مغلق" }, tone: "success" },
  ],
  actions: {
    draft: [{ key: "approve", label: { en: "Approve", ar: "اعتماد" }, variant: "primary" }],
    approved: [{ key: "prepare_dispatch", label: { en: "Prepare Dispatch", ar: "تجهيز الإرسال" }, variant: "primary" }],
    prepare_dispatch: [{ key: "post_outbound", label: { en: "Post Outbound", ar: "ترحيل" }, variant: "primary" }],
    posted: [
      { key: "reverse", label: { en: "Reverse", ar: "عكس" }, variant: "danger", danger: true, requireReason: true },
      { key: "close", label: { en: "Close", ar: "إغلاق" }, variant: "primary" },
    ],
  },
  stepIndex: (status: string) => ["draft", "approved", "prepare_dispatch", "posted", "closed"].indexOf(status),
}

export const purchaseReturnStatusTone: Record<string, any> = {
  draft: "neutral", approved: "info", prepare_dispatch: "pending", posted: "processing", closed: "success", cancelled: "danger", reversed: "warning",
}

export const purchaseReturnStatusLabel: Record<string, { en: string; ar: string }> = {
  draft: { en: "Draft", ar: "مسودة" },
  approved: { en: "Approved", ar: "معتمد" },
  prepare_dispatch: { en: "Prepare Dispatch", ar: "تجهيز" },
  posted: { en: "Posted", ar: "مرحل" },
  closed: { en: "Closed", ar: "مغلق" },
  cancelled: { en: "Cancelled", ar: "ملغي" },
  reversed: { en: "Reversed", ar: "معكوس" },
}

export const platformAllocationWorkflow: WorkflowDefinition = {
  steps: [
    { status: "draft", label: { en: "Create", ar: "إنشاء" }, tone: "neutral" },
    { status: "approved", label: { en: "Approve", ar: "اعتماد" }, tone: "info" },
    { status: "posted", label: { en: "Transfer to INV-PLATFORM", ar: "نقل" }, tone: "processing" },
    { status: "closed", label: { en: "Available / Closed", ar: "متاح / مغلق" }, tone: "success" },
  ],
  actions: {
    draft: [{ key: "approve", label: { en: "Approve", ar: "اعتماد" }, variant: "primary" }],
    approved: [{ key: "post", label: { en: "Post", ar: "ترحيل" }, variant: "primary" }],
    posted: [
      { key: "withdraw", label: { en: "Withdraw from Platform", ar: "سحب" }, variant: "outline" },
      { key: "close", label: { en: "Close", ar: "إغلاق" }, variant: "primary" },
    ],
  },
  stepIndex: (status: string) => ["draft", "approved", "posted", "closed"].indexOf(status),
}

export const platformAllocationStatusTone: Record<string, any> = {
  draft: "neutral", approved: "info", posted: "processing", withdrawn: "warning", closed: "success", cancelled: "danger",
}

export const platformAllocationStatusLabel: Record<string, { en: string; ar: string }> = {
  draft: { en: "Draft", ar: "مسودة" },
  approved: { en: "Approved", ar: "معتمد" },
  posted: { en: "Posted", ar: "مرحل" },
  withdrawn: { en: "Withdrawn", ar: "مسحوب" },
  closed: { en: "Closed", ar: "مغلق" },
  cancelled: { en: "Cancelled", ar: "ملغي" },
}
