export type ReceivingBatchRead = {
  id: string
  batch_number: string
  shipment_number?: string | null
  supplier_id?: string | null
  purchase_order_id?: string | null
  branch_id: string
  vault_id?: string | null
  expected_total_bars?: number | null
  actual_total_bars?: number | null
  expected_total_weight?: number | null
  actual_total_weight?: number | null
  documents?: Record<string, unknown> | null
  notes?: string | null
  status: string
  received_by?: string | null
  received_at?: string | null
  created_at: string
  updated_at: string
}

export type ReceivingBatchCreatePayload = {
  batch_number?: string | null
  shipment_number?: string | null
  supplier_id?: string | null
  purchase_order_id?: string | null
  branch_id: string
  vault_id?: string | null
  expected_total_bars?: number | null
  expected_total_weight?: number | null
  documents?: Record<string, unknown> | null
  notes?: string | null
}

export type ReceivingBatchCompletePayload = {
  actual_total_bars: number
  actual_total_weight: number
}

export type ReceivingBatchFormValues = {
  batch_number: string
  shipment_number: string
  supplier_id: string
  purchase_order_id: string
  branch_id: string
  vault_id: string
  expected_total_bars: number | ""
  expected_total_weight: number | ""
  notes: string
}

export type ReceivingBatchCompleteFormValues = {
  actual_total_bars: number | ""
  actual_total_weight: number | ""
}

export type ReceivingOrderWorkflowAction =
  | "submit-for-review"
  | "approve"
  | "reject"
  | "mark-received"
  | "post"
  | "cancel"
  | "resubmit"

export type ReceivingOrderItemRead = {
  id: string
  receiving_order_id: string
  metal_type: string
  purity_id?: string | null
  purity_name?: string | null
  product_id?: string | null
  product_name?: string | null
  unit_id?: string | null
  unit_name?: string | null
  declared_weight_grams: string
  measured_weight_grams?: string | null
  accepted_weight_grams?: string | null
  weight_difference?: string | null
  price_per_gram?: string
  cost_per_gram?: string
  quantity: number
  premium?: string
  line_total: string
  condition_grade?: string | null
  assay_result_id?: string | null
  notes?: string | null
}

export type ReceivingOrderRead = {
  id: string
  receiving_order_no: string
  order_type: string
  priority: string
  status: string
  customer_id?: string | null
  customer_name?: string | null
  customer_code?: string | null
  supplier_id?: string | null
  supplier_name?: string | null
  supplier_no?: string | null
  user_id?: string | null
  currency_id?: string | null
  currency_code?: string | null
  branch_id?: string | null
  branch_name?: string | null
  vault_id?: string | null
  vault_name?: string | null
  estimated_weight_grams?: string | null
  actual_weight_grams?: string | null
  weight_difference?: string | null
  purity_id?: string | null
  purity_name?: 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
  received_at?: string | null
  posted_at?: string | null
  posting_journal_id?: string | null
  rejected_at?: string | null
  cancelled_at?: string | null
  cancellation_reason?: string | null
  items: ReceivingOrderItemRead[]
  created_at: string
  updated_at: string
}

export type ReceivingOrderCreatePayload = {
  order_type?: string
  priority?: string
  customer_id?: string | null
  supplier_id?: string | null
  currency_id?: string | null
  branch_id?: string | null
  vault_id?: string | null
  estimated_weight_grams?: number | null
  purity_id?: string | null
  condition_grade?: string | null
  notes?: string | null
  source_type?: string | null
  source_id?: string | null
  items?: ReceivingOrderItemCreatePayload[]
}

export type ReceivingOrderItemCreatePayload = {
  purity_id?: string | null
  product_id?: string | null
  unit_id?: string | null
  declared_weight_grams: number
  price_per_gram?: number
  cost_per_gram?: number
  quantity?: number
  premium?: number
  condition_grade?: string | null
  notes?: string | null
}

export type ReceivingOrderUpdatePayload = Partial<Pick<
  ReceivingOrderCreatePayload,
  "priority" | "branch_id" | "vault_id" | "notes"
>>

export type ReceivingPostingResult = {
  order_id: string
  order_no: string
  journal_entries: Record<string, unknown>[]
  gold_postings: Record<string, unknown>[]
  pool_updates: Record<string, unknown>[]
  custody_batch_id?: string | null
  inventory_movement_ids: string[]
}

export type ReceivingOrderStatusHistoryRead = {
  id: string
  receiving_order_id: string
  old_status?: string | null
  new_status: string
  note?: string | null
  changed_by?: string | null
  changed_at: string
}

export type AssayResultCreatePayload = {
  method?: string | null
  purity_id?: string | null
  purity_ratio?: number | null
  net_gold_grams?: number | null
  impurities_grams?: number | null
  final_weight_grams?: number | null
  grade?: string | null
  difference_grams?: number | null
  result?: string | null
  remarks?: string | null
  report_file_id?: string | null
  notes?: string | null
}

export type ReceivingPostingPreview = {
  order_id: string
  order_no: string
  can_post: boolean
  status: string
  expected_journals: Record<string, unknown>[]
  gold_movements: Record<string, unknown>[]
  custody_updates: Record<string, unknown>[]
  warnings: string[]
}

export type ReceivingReceiptRead = {
  order_id: string
  order_no: string
  status: string
  customer_id?: string | null
  customer_name?: string | null
  customer_code?: string | null
  supplier_id?: string | null
  supplier_name?: string | null
  supplier_no?: string | null
  branch_id?: string | null
  branch_name?: string | null
  vault_id?: string | null
  vault_name?: string | null
  estimated_weight_grams?: string | null
  actual_weight_grams?: string | null
  weight_difference?: string | null
  fee_amount: string
  tax_amount: string
  total_amount: string
  approved_at?: string | null
  received_at?: string | null
  posted_at?: string | null
  posting_journal_id?: string | null
  items: ReceivingOrderItemRead[]
  status_history: ReceivingOrderStatusHistoryRead[]
}
