export type OrderSide = "buy" | "sell"

export type OrderWorkflowAction =
  | "submit-for-review"
  | "submit-for-approval"
  | "approve"
  | "reject"
  | "cancel"
  | "post"

export type OrderItemRead = {
  id: string
  weight_grams: string
  quantity: number
  price_per_gram: string
  line_total: string
  notes?: string | null
}

export type BuyOrderRead = {
  id: string
  buy_order_no: string
  buy_order_type: string
  priority: string
  status: string
  supplier_id: string | null
  customer_id: string | null
  user_id: string | null
  currency_id: string | null
  target_branch_id: string | null
  target_vault_id: string | null
  notes: string | null
  subtotal_amount: string
  fee_amount: string
  tax_amount: string
  discount_amount: string
  total_amount: string
  reviewed_at: 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
  items: OrderItemRead[]
}

export type BuyOrderCreatePayload = {
  buy_order_type?: string
  priority?: string
  supplier_id?: string | null
  customer_id?: string | null
  currency_id?: string | null
  target_branch_id?: string | null
  target_vault_id?: string | null
  notes?: string | null
}

export type BuyOrderUpdatePayload = Partial<BuyOrderCreatePayload>

export type SellOrderRead = {
  id: string
  sell_order_no: string
  sell_order_type: string
  priority: string
  status: string
  customer_id: string
  user_id: string | null
  currency_id: string | null
  branch_id: string | null
  vault_id: string | null
  notes: string | null
  subtotal_amount: string
  fee_amount: string
  tax_amount: string
  discount_amount: string
  total_amount: string
  price_snapshot: Record<string, unknown> | null
  expires_at: string | null
  reviewed_at: 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
  items: OrderItemRead[]
}

export type SellOrderCreatePayload = {
  sell_order_type?: string
  priority?: string
  customer_id: string
  currency_id?: string | null
  branch_id?: string | null
  vault_id?: string | null
  notes?: string | null
}

export type SellOrderUpdatePayload = Partial<Omit<SellOrderCreatePayload, "customer_id">>

export type CombinedOrderRead = {
  id: string
  order_no: string
  order_type: OrderSide
  workflow_type: string
  status: string
  priority: string
  user_id: string | null
  customer_id: string | null
  customer_name: string | null
  branch_id: string | null
  vault_id: string | null
  currency_id: string | null
  requested_grams: string | null
  price_per_gram: string | null
  subtotal_amount: string
  fee_amount: string
  tax_amount: string
  discount_amount: string
  total_amount: string
  notes: string | null
  price_snapshot?: Record<string, unknown> | null
  expires_at?: string | null
  reviewed_at: 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 OrderPostingResult = {
  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>[]
}
