export type SellOrderStatus =
  | "draft"
  | "pending_review"
  | "pending_approval"
  | "approved"
  | "posted"
  | "delivering"
  | "delivered"
  | "rejected"
  | "cancelled"
  | "closed"

export type SellPermission =
  | "sell_orders:read"
  | "sell_orders:create"
  | "sell_orders:update"
  | "sell_orders:review"
  | "sell_orders:approve"
  | "sell_orders:post"
  | "sell_orders:cancel"
  | "sell_orders:reverse"

export type NamedSummary = { id: string; code?: string | null; name_en?: string | null; name_ar?: string | null }
export type CustomerSummary = { id: string; customer_code?: string | null; full_name_en?: string | null; full_name_ar?: string | null }
export type UserSummary = { id: string; full_name?: string | null; email?: string | null }
export type VaultSummary = { id: string; code?: string | null; name?: string | null; vault_type?: string | null }
export type PuritySummary = { id: string; karat?: number | null; name_en?: string | null; name_ar?: string | null }

export type SellOrderItem = {
  id: string
  sell_order_id: string
  metal_type: string
  purity_id: string | null
  product_id: string | null
  unit_id: string | null
  weight_grams: string
  quantity: number
  price_per_gram: string
  premium: string
  line_total: string
  pool_id: string | null
  cost_per_gram: string
  notes: string | null
  created_at: string
  purity?: PuritySummary | null
}

export type SellOrderItemPayload = {
  metal_type?: string
  purity_id: string
  product_id?: string | null
  unit_id?: string | null
  weight_grams: number
  quantity?: number
  price_per_gram?: number
  premium?: number
  notes?: string | null
}

export type SellOrderListItem = {
  id: string
  sell_order_no: string
  sell_order_type: string
  priority: string
  status: SellOrderStatus
  customer_id: string
  customer?: CustomerSummary | null
  user_id?: string | null
  created_by?: UserSummary | null
  currency_id?: string | null
  currency?: NamedSummary | null
  branch_id?: string | null
  branch?: NamedSummary | null
  vault_id?: string | null
  vault?: VaultSummary | null
  subtotal_amount: string
  fee_amount: string
  tax_amount: string
  discount_amount: string
  total_amount: string
  price_locked_at?: string | null
  expires_at?: string | null
  reviewed_by?: string | null
  reviewed_at?: string | null
  approved_by?: string | null
  approved_at?: string | null
  posted_at?: string | null
  rejected_at?: string | null
  cancelled_at?: string | null
  cancellation_reason?: string | null
  created_at: string
  updated_at: string
}

export type SellOrderDetail = SellOrderListItem & {
  exchange_rate?: string | null
  notes?: string | null
  price_snapshot?: Record<string, unknown> | null
  posting_journal_id?: string | null
  closed_at?: string | null
  items: SellOrderItem[]
  reviewer?: UserSummary | null
  approver?: UserSummary | null
}

export type SellOrderCreateRequest = {
  sell_order_type?: "customer_sale"
  priority?: "low" | "normal" | "high" | "urgent"
  customer_id: string
  currency_id: string
  branch_id: string
  vault_id: string
  notes?: string | null
  items: SellOrderItemPayload[]
}

export type SellOrderFilters = {
  page?: number
  page_size?: number
  search?: string
  status?: string
  branch_id?: string
  vault_id?: string
  purity_id?: string
  customer_id?: string
  currency_id?: string
  date_from?: string
  date_to?: string
}

export type SellPrice = {
  mid_price: string
  bid_price: string
  ask_price: string
  buy_spread_percent: string
  sell_spread_percent: string
  min_spread_amount: string
  sell_price_per_gram: string
  source_id: string
  market_time: string
}

export type SellOrderAvailability = {
  sell_order_id: string
  is_available: boolean
  price_valid: boolean
  price_expires_at?: string | null
  customer_eligible: boolean
  branch_vault_valid: boolean
  customer_cash?: {
    wallet_id?: string | null
    currency_code?: string | null
    available_before?: string | null
    required_amount?: string | null
    available_after?: string | null
  } | null
  gold_wallets: Array<{
    wallet_id?: string | null
    purity_id?: string | null
    purity_name?: string | null
    available_before_grams?: string | null
    required_grams?: string | null
    available_after_grams?: string | null
  }>
  inventory: Array<{
    pool_id?: string | null
    purity_id?: string | null
    purity_name?: string | null
    vault_id?: string | null
    available_grams?: string | null
    reserved_grams?: string | null
    required_grams?: string | null
  }>
  blocking_reasons: string[]
}

export type StatusHistory = { id: string; old_status?: string | null; new_status: string; note?: string | null; changed_at: string; changed_by_user?: UserSummary | null }
export type ApprovalStep = { id: string; step_order: number; status: string; decision_note?: string | null; decided_at?: string | null; approver?: UserSummary | null }
export type ApprovalRequest = { id: string; approval_type: string; status: string; requested_at?: string | null; resolved_at?: string | null; decision_note?: string | null; steps: ApprovalStep[]; requester?: UserSummary | null; resolver?: UserSummary | null }
export type WalletMovement = { wallet_id: string; wallet_type: string; currency_code?: string | null; purity_name?: string | null; entry_type: string; amount?: string | null; grams?: string | null; balance_before?: string | null; balance_after?: string | null; ledger_entry_id: string; created_at?: string | null }
export type InventoryMovement = { id: string; movement_type: string; from_vault_id?: string | null; to_vault_id?: string | null; from_status?: string | null; to_status?: string | null; reason?: string | null; quantity?: string | null; created_at?: string | null }
export type JournalLine = { id: string; account_code?: string | null; account_name?: string | null; amount: string; gold_grams?: string | null; entry_type: string; description?: string | null }
export type JournalHeader = { id: string; journal_number: string; journal_date?: string | null; description?: string | null; status: string; total_amount?: string | null; reversal_of_id?: string | null; lines: JournalLine[] }
export type SellOrderVoucher = { order_id: string; order_no: string; order_type: string; status: string; customer?: CustomerSummary | null; branch?: NamedSummary | null; vault?: VaultSummary | null; currency?: NamedSummary | null; items: SellOrderItem[]; subtotal_amount: string; fee_amount: string; tax_amount: string; discount_amount: string; total_amount: string; created_by?: UserSummary | null; reviewed_by?: UserSummary | null; approved_by?: UserSummary | null; created_at?: string | null; reviewed_at?: string | null; approved_at?: string | null; posted_at?: string | null }

export type SellOrderSupportData = {
  availability: SellOrderAvailability | null
  history: StatusHistory[]
  approvals: ApprovalRequest[]
  walletMovements: WalletMovement[]
  inventoryMovements: InventoryMovement[]
  journals: JournalHeader[]
  auditLogs: Array<Record<string, any>>
  voucher: SellOrderVoucher | null
}
