export type PurchaseOrderStatus =
  | "draft"
  | "pending_review"
  | "pending_approval"
  | "approved"
  | "sent_to_supplier"
  | "partially_supplied"
  | "fully_supplied"
  | "closed"
  | "cancelled"
  | "rejected"

export type PurchaseOrderItem = {
  id: string
  buy_order_id: string
  metal_type: string
  purity_id: string | null
  product_id: string | null
  unit_id: string | null
  weight_grams: string
  quantity: number
  pricing_basis: "per_gram" | "per_unit"
  price_per_gram: string
  premium: string
  line_total: string
  notes: string | null
  created_at: string
}

export type PurchaseOrder = {
  id: string
  buy_order_no: string
  buy_order_type: string
  priority: string
  status: PurchaseOrderStatus
  supplier_id: string | null
  customer_id: string | null
  user_id: string | null
  currency_id: string | null
  exchange_rate: string | null
  target_branch_id: string | null
  target_vault_id: string | null
  purchase_request_no: string | null
  expected_delivery_date: string | null
  payment_terms: string | null
  delivery_terms: string | null
  cost_center: string | null
  notes: string | null
  subtotal_amount: string
  fee_amount: string
  tax_amount: string
  discount_amount: string
  total_amount: string
  reviewed_by: string | null
  reviewed_at: string | null
  approved_by: string | null
  approved_at: string | null
  sent_at: string | null
  closed_at: string | null
  cancelled_at: string | null
  cancellation_reason: string | null
  posted_at: string | null
  posting_journal_id: string | null
  rejected_at: string | null
  supplier_reference: string | null
  external_reference: string | null
  created_at: string
  updated_at: string
  items: PurchaseOrderItem[]
}

export type PurchaseOrderCreatePayload = {
  priority?: string
  supplier_id?: string
  currency_id?: string
  exchange_rate?: number
  target_branch_id?: string
  target_vault_id?: string
  purchase_request_no?: string
  expected_delivery_date?: string
  payment_terms?: string
  delivery_terms?: string
  cost_center?: string
  supplier_reference?: string
  external_reference?: string
  notes?: string
  items: Array<{
    metal_type?: string
    purity_id?: string
    product_id?: string
    unit_id?: string
    weight_grams: number
    quantity?: number
    pricing_basis?: "per_gram" | "per_unit"
    price_per_gram?: number
    premium?: number
    notes?: string
  }>
}

export type PurchaseOrderUpdatePayload = Partial<Omit<PurchaseOrderCreatePayload, "items">>

export type PurchaseOrderFilters = {
  page?: number
  page_size?: number
  search?: string
  status_filter?: string
  supplier_id?: string
  branch_id?: string
  currency_id?: string
}

export type AdvancePaymentPayload = {
  amount: number
  currency_id?: string
  payment_method?: string
  reference?: string
  notes?: string
}

export type AdvancePayment = {
  id: string
  order_id: string
  amount: string
  currency_id: string | null
  currency_code: string | null
  payment_method: string | null
  reference: string | null
  notes: string | null
  recorded_by: string | null
  created_at: string
}

export type AccountingPreviewLine = {
  account_code: string
  account_name: string
  entry_type: string
  amount: string
  description: string | null
}

export type AccountingPreview = {
  order_id: string
  order_no: string
  total_amount: string
  currency_code: string | null
  subtotal: string
  fees: string
  taxes: string
  discount: string
  advance_paid: string
  remaining: string
  lines: AccountingPreviewLine[]
}

export type PurchaseOrderStatusHistory = {
  id: string
  buy_order_id: string
  old_status: string | null
  new_status: string
  note: string | null
  changed_by: string | null
  changed_at: string
}
