/**
 * 採購單相關型別定義
 */

export type PurchaseOrderStatus =
    | "draft"           // 草稿
    | "pending"         // 簽核中
    | "approved"        // 已核准
    | "partial"         // 部分收貨
    | "completed"       // 全數收貨
    | "closed"          // 已結案
    | "cancelled";      // 已作廢



export type PaymentMethod = "cash" | "bank_transfer" | "credit_card" | "check"; // 付款方式

export type InvoiceType = "duplicate" | "triplicate" | "electronic"; // 發票類型

export interface PurchaseOrderItem {
    productId: string;
    productName: string;
    quantity: number;
    unitId?: number; // 選擇的單位ID
    base_unit_id?: number;
    base_unit_name?: string;
    large_unit_id?: number;
    large_unit_name?: string;
    purchase_unit_id?: number;
    conversion_rate?: number;
    selectedUnit?: 'base' | 'large'; // 前端狀態輔助
    unitPrice: number;
    previousPrice?: number;
    subtotal: number;
}

// 審核資訊
export interface ReviewInfo {
    reviewedBy: string;      // 審核人
    reviewedAt: string;      // 審核時間
    rejectionReason?: string; // 退回原因（僅退回時）
}

// 發票資訊
export interface InvoiceInfo {
    invoiceNumber: string;     // 發票號碼
    invoiceAmount: number;     // 發票金額
    invoiceDate: string;       // 發票日期
    invoiceType: InvoiceType;  // 發票類型
    companyName?: string;      // 公司抬頭（三聯式）
    taxId?: string;            // 統一編號（三聯式）
}

// 付款資訊
export interface PaymentInfo {
    paymentMethod: PaymentMethod; // 付款方式
    paymentDate: string;          // 付款日期
    actualAmount: number;         // 實際付款金額
    paidBy: string;               // 付款人
    paidAt: string;               // 付款記錄時間
    hasInvoice: boolean;          // 是否有發票
    invoice?: InvoiceInfo;        // 發票資訊（如果有）
}

export interface PurchaseOrder {
    id: string;
    poNumber: string;
    supplierId: string;
    supplierName: string;
    orderDate?: string; // 採購日期
    expectedDate: string;
    status: PurchaseOrderStatus;
    items: PurchaseOrderItem[];
    totalAmount: number;
    createdAt: string;
    createdBy: string;      // 申請人（採購人）
    warehouse_id: number;    // 預計入庫倉庫 ID
    warehouse_name: string;  // 預計入庫倉庫名稱
    remark?: string;
    reviewInfo?: ReviewInfo; // 審核資訊
    paymentInfo?: PaymentInfo; // 付款資訊
    invoiceNumber?: string;  // 發票號碼
    invoiceDate?: string;    // 發票日期
    invoiceAmount?: number;  // 發票金額
    tax_amount?: number;     // 稅額 (DB column)
    taxAmount?: number;      // 稅額 (Accessor)
    grand_total?: number;    // 總計 (含稅) (DB column)
    grandTotal?: number;     // 總計 (含稅) (Accessor)
}

export interface CommonProduct {
    productId: string;
    productName: string;
    base_unit_id?: number;
    base_unit_name?: string;
    large_unit_id?: number;
    large_unit_name?: string;
    purchase_unit_id?: number;
    conversion_rate?: number;
    lastPrice: number;
}

export interface Supplier {
    id: number | string;
    name: string;
    contact?: string;
    phone?: string;
    email?: string;
    commonProducts: CommonProduct[];
}

export interface InspectionItem extends PurchaseOrderItem {
    receivedQuantity: number;
    damagedQuantity: number; // 保留欄位以維持相容性，但不再使用
    shortageQuantity: number;
    issueType?: "shortage" | "none";
    issueNote?: string;
}
