import type {
  ApprovalRequest,
  CustomerSummary,
  InventoryMovement,
  JournalHeader,
  NamedSummary,
  PuritySummary,
  StatusHistory,
  UserSummary,
  VaultSummary,
  WalletMovement,
} from "./sell-orders"

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

export type BuyPermission =
  | "buy_orders:read"
  | "buy_orders:create"
  | "buy_orders:update"
  | "buy_orders:review"
  | "buy_orders:approve"
  | "buy_orders:post"
  | "buy_orders:cancel"
  | "buy_orders:reverse"
  | "buy_orders:attachments"
  | "buy_orders:print"

export type BuyOrderType = "supplier" | "customer_buyback"

export type BuyOrderItem = {
  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
  price_per_gram: string
  premium: string
  line_total: string
  notes: string | null
  created_at: string
  purity?: PuritySummary | null
}

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

export type BuyOrderListItem = {
  id: string
  buy_order_no: string
  buy_order_type: BuyOrderType | string
  priority: string
  status: BuyOrderStatus
  supplier_id?: string | null
  supplier?: NamedSummary | null
  customer_id?: string | null
  customer?: CustomerSummary | null
  user_id?: string | null
  created_by?: UserSummary | null
  currency_id?: string | null
  currency?: NamedSummary | null
  target_branch_id?: string | null
  target_branch?: NamedSummary | null
  target_vault_id?: string | null
  target_vault?: VaultSummary | null
  purchase_request_no?: string | null
  expected_delivery_date?: string | null
  payment_terms?: string | null
  delivery_terms?: string | null
  cost_center?: string | null
  supplier_reference?: string | null
  external_reference?: 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
  posted_at?: string | null
  closed_at?: string | null
  rejected_at?: string | null
  cancelled_at?: string | null
  cancellation_reason?: string | null
  created_at: string
  updated_at: string
}

export type BuyOrderDetail = BuyOrderListItem & {
  exchange_rate?: string | null
  notes?: string | null
  posting_journal_id?: string | null
  items: BuyOrderItem[]
  reviewer?: UserSummary | null
  approver?: UserSummary | null
}

export type BuyOrderCreateRequest = {
  buy_order_type?: BuyOrderType
  priority?: "low" | "normal" | "high" | "urgent"
  supplier_id?: string | null
  customer_id?: string | null
  currency_id?: string | null
  exchange_rate?: number | 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
  supplier_reference?: string | null
  external_reference?: string | null
  notes?: string | null
  items: BuyOrderItemPayload[]
}

export type BuyOrderFilters = {
  page?: number
  page_size?: number
  search?: string
  status?: string
  buy_order_type?: string
  supplier_id?: string
  customer_id?: string
  currency_id?: string
  branch_id?: string
  vault_id?: string
  date_from?: string
  date_to?: string
}

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

export type BuyOrderPostingResult = {
  order_id: string
  order_no: string
  journal_entries?: Record<string, unknown>[]
  cash_postings?: Record<string, unknown>[]
  gold_postings?: Record<string, unknown>[]
  applied_fees_total?: string | null
  applied_taxes_total?: string | null
  pool_updates?: Record<string, unknown>[]
  reversed_journals?: Record<string, unknown>[]
  reversed_gold_entries?: number
  reversed_cash_entries?: number
  reversed_fees?: number
  reversed_movements?: number
}

export type BuyOrderVoucher = {
  order_id: string
  order_no: string
  order_type: string
  status: string
  supplier_id?: string | null
  customer_id?: string | null
  currency?: NamedSummary | null
  items: BuyOrderItem[]
  subtotal_amount: string
  fee_amount: string
  tax_amount: string
  discount_amount: string
  total_amount: string
  created_at?: string | null
  approved_at?: string | null
  posted_at?: string | null
}

export type BuyOrderSupportData = {
  history: StatusHistory[]
  approvals: ApprovalRequest[]
  postings: BuyOrderPostingResult | null
  walletMovements: WalletMovement[]
  inventoryMovements: InventoryMovement[]
  journals: JournalHeader[]
  auditLogs: Array<Record<string, any>>
  voucher: BuyOrderVoucher | null
}

