import { IngredientItem } from './ingredient'
import { Vendor } from './vendor'

export interface PurchaseOrderRequest {
  vendor_id: string // uuid.UUID
  po_number: string
  transaction_date: string // ISO date string
  due_date: string // ISO date string
  reference?: string
  status?: 'draft' | 'sent' | 'approved' | 'received' | 'cancelled'
  message?: string
  items: PurchaseOrderItemRequest[]
  attachment_file_ids?: string[] // uuid.UUID[]
}

export interface PurchaseOrderItemRequest {
  ingredient_id: string // uuid.UUID
  description?: string
  quantity: number
  unit_id: string // uuid.UUID
  amount: number
}

export interface PurchaseOrders {
  purchase_orders: PurchaseOrder[]
  total_count: number
  page: number
  limit: number
  total_pages: number
}

export interface PurchaseOrder {
  id: string
  organization_id: string
  vendor_id: string
  po_number: string
  transaction_date: string // RFC3339
  due_date: string // RFC3339
  reference: string | null
  status: string
  message: string | null
  total_amount: number
  created_at: string
  updated_at: string
  vendor: Vendor
  items: PurchaseOrderItem[]
  attachments: PurchaseOrderAttachment[]
}

export interface PurchaseOrderItem {
  id: string
  purchase_order_id: string
  ingredient_id: string
  description: string
  quantity: number
  unit_id: string
  amount: number
  created_at: string
  updated_at: string
  ingredient: PurchaseOrderIngredient
  unit: PurchaseOrderUnit
}

export interface PurchaseOrderIngredient {
  id: string
  name: string
}

export interface PurchaseOrderUnit {
  id: string
  name: string
}

export interface PurchaseOrderAttachment {
  id: string
  purchase_order_id: string
  file_id: string
  created_at: string
  file: PurchaseOrderFile
}

export interface PurchaseOrderFile {
  id: string
  organization_id: string
  user_id: string
  file_name: string
  original_name: string
  file_url: string
  file_size: number
  mime_type: string
  file_type: string
  upload_path: string
  is_public: boolean
  created_at: string
  updated_at: string
}

export interface PurchaseOrderFormData {
  vendor: { label: string; value: string } | null
  po_number: string
  transaction_date: string
  due_date: string
  reference: string
  status: 'draft' | 'sent' | 'approved' | 'received' | 'cancelled'
  showPesan: boolean
  showAttachment: boolean
  message: string
  items: PurchaseOrderFormItem[]
  attachment_file_ids: string[]
}

export interface PurchaseOrderFormItem {
  id: number // for UI tracking
  ingredient: { label: string; value: string; originalData?: IngredientItem } | null
  description: string
  quantity: number
  unit: { label: string; value: string } | null
  amount: number
  total: number // calculated field for UI
}

export interface SendPaymentPurchaseOrderRequest {
  reference?: string
  status?: 'received'
  attachment_file_ids?: string[] // uuid.UUID[]
}
