import type { PurchaseOrderItem, PurchaseOrderDocument, PurchaseOrderPaymentMethod, RealEstateAllocationMethod } from '@/lib/types';

export type PurchasePostingStatus = 'draft' | 'saved' | 'posted';
export type PurchaseTaxMethod = 'tax-inclusive' | 'invoice-level' | 'line-level' | 'tax-exempt';
export type PurchaseTransferCreationMode = 'direct_internal_transfer' | 'work_task' | 'none';

export type PurchaseCreateCommand = {
  supplierId: string;
  supplierName: string;
  supplierInvoiceNumber?: string;
  supplierInvoiceDate?: string;
  warehouseId?: string;
  realEstateLinkMode?: 'none' | 'order' | 'line-items';
  realEstateProjectId?: string;
  realEstateProjectName?: string;
  realEstateScope?: 'project' | 'floor' | 'part';
  realEstateFloorLabel?: string;
  realEstatePartLabel?: string;
  realEstateAllocationMethod?: RealEstateAllocationMethod;
  allowDuplicateInvoice?: boolean;
  notes?: string;
  documents: PurchaseOrderDocument[];
  items: Array<{
    materialId: string;
    name: string;
    quantity: number;
    unitPrice: number;
    total: number;
    lineDiscountPercent?: number;
    lineDiscountAmount?: number;
    lineDiscount?: number;
    bonus?: number;
    warehouseId?: string;
    shelfId?: string;
    taxRate?: number;
    taxAmount?: number;
    realEstateProjectId?: string;
    realEstateProjectName?: string;
    realEstateScope?: 'project' | 'floor' | 'part';
    realEstateFloorLabel?: string;
    realEstatePartLabel?: string;
    realEstateAllocationMethod?: RealEstateAllocationMethod;
  }>;
  taxMethod: PurchaseTaxMethod;
  documentType?: 'shipment' | 'tax_invoice';
  linkedShipmentIds?: string[];
  taxInvoiceId?: string;
  postingStatus: PurchasePostingStatus;
  purchaseTransferCreationMode?: PurchaseTransferCreationMode;
  invoiceTaxRate: number;
  invoiceDiscountPercent: number;
  invoiceDiscountAmount: number;
  amountPaid: number;
  paymentMethods: PurchaseOrderPaymentMethod[];
  currencyCode: string;
  baseCurrencyCode: string;
  exchangeRate: number;
  rateDate: string;
  rateSource?: string;
  isAutoRate?: boolean;
};

export type TaxRateResolver = (materialId: string) => number;

export type PurchaseCalculatedTotals = {
  processedItems: PurchaseOrderItem[];
  subTotal: number;
  discountedSubTotal: number;
  lineDiscountTotal: number;
  invoiceDiscountAmount: number;
  totalDiscount: number;
  taxAmount: number;
  grandTotal: number;
  amountDue: number;
};
