/**
 * Public shapes for warehouse fee invoices.
 *
 * Warehouse fee invoices are no longer a standalone table — they are global
 * `Invoice` rows (`source = "warehouse"`, `sourceId = inventoryId`) owned by the
 * central {@link BillingService}. These types preserve the warehouse-facing API
 * contract: `WarehouseInvoiceService` reshapes the global invoice (+ lines +
 * inventory context) back into the historical `WarehouseFeeInvoice` JSON so the
 * portal/backoffice stay untouched.
 */

export const WAREHOUSE_INVOICE_TYPES = ['DEMURRAGE', 'STORAGE_FEE', 'MIXED_WAREHOUSE_FEES'] as const;
export type WarehouseInvoiceType = (typeof WAREHOUSE_INVOICE_TYPES)[number];

export const WAREHOUSE_INVOICE_STATUSES = [
  'DRAFT',
  'ISSUED',
  'PARTIALLY_PAID',
  'PAID',
  'CANCELLED',
] as const;
export type WarehouseInvoiceStatus = (typeof WAREHOUSE_INVOICE_STATUSES)[number];

export const WAREHOUSE_FEE_TYPES = [
  'CONTAINER_DEMURRAGE',
  'BULK_DEMURRAGE',
  'STORAGE_FEE',
  'HANDLING_FEE',
  'DOUBLE_HANDLING',
  'TRUCK_DETENTION',
] as const;
export type WarehouseFeeType = (typeof WAREHOUSE_FEE_TYPES)[number];

/** A single recorded payment against a warehouse fee invoice (history). */
export interface WarehouseInvoicePayment {
  amount: number;
  method?: string | null;
  reference?: string | null;
  paidAt: string;
}

/** A billed warehouse fee line, projected from a global `InvoiceLine`. */
export interface WarehouseInvoiceItemView {
  feeRuleId: string | null;
  feeType: WarehouseFeeType;
  description: string;
  quantity: number;
  unitRate: number;
  amount: number;
  currency: string;
  chargeableDays: number | null;
  freeDays: number | null;
}

/**
 * The warehouse-facing invoice header — same field set the old
 * `WarehouseFeeInvoice` entity exposed, projected from a global `Invoice`. The
 * typed FKs (`bookingId`/`facilityId`/`warehouseId`/`yardId`/`zoneId`) and the
 * charge `period` are derived from the linked inventory item; `customerId` is the
 * billed company; `invoiceType` is the invoice `type`.
 */
export interface WarehouseFeeInvoiceView {
  id: string;
  invoiceNumber: string;
  bookingId: string | null;
  customerId: string | null;
  inventoryId: string;
  facilityId: string | null;
  warehouseId: string | null;
  yardId: string | null;
  zoneId: string | null;
  invoiceType: WarehouseInvoiceType;
  status: WarehouseInvoiceStatus;
  subtotalAmount: number;
  taxAmount: number;
  totalAmount: number;
  paidAmount: number;
  balanceAmount: number;
  currency: string;
  periodStart: Date | null;
  periodEnd: Date | null;
  issuedAt: Date | null;
  dueDate: Date | null;
  paidAt: Date | null;
  cancelledAt: Date | null;
  payments: WarehouseInvoicePayment[];
  notes: string | null;
  createdAt: Date;
  updatedAt: Date;
}
