// ─── Sales Invoice Item ─────────────────────────────────────────

export interface SalesInvoiceItem {
  id: number;
  sales_invoice_id: number;
  product_id: number;
  product_name: string;
  product_code: string;
  product_variant_id: number | null;
  variant_name: string | null;
  unit_id: number;
  unit_name: string;
  unit_symbol: string;
  warehouse_id: number | null;
  warehouse_name: string | null;
  description: string | null;
  description_ar: string | null;
  quantity: string;
  unit_price: string;
  discount_percent: string;
  discount_amount: string;
  tax_rate_id: number | null;
  tax_rate_name: string | null;
  tax_rate_percent: string;
  tax_amount: string;
  line_total: string;
  unit_cost: string;
  total_cost: string;
  notes: string | null;
  sort_order: number;
}

export interface SalesInvoiceItemPayload {
  product_id: number;
  product_variant_id?: number | null;
  unit_id: number;
  warehouse_id?: number | null;
  description?: string | null;
  description_ar?: string | null;
  quantity: number;
  unit_price: number;
  discount_percent?: number;
  discount_amount?: number;
  tax_rate_id?: number | null;
  notes?: string | null;
  sort_order?: number;
}

// ─── Sales Invoice ──────────────────────────────────────────────

export interface SalesInvoice {
  id: number;
  invoice_number: string;
  date: string;
  due_date: string;
  customer_id: number;
  customer_name: string;
  customer_code: string;
  salesperson_id: number | null;
  salesperson_name: string | null;
  warehouse_id: number | null;
  warehouse_name: string | null;
  sales_order_id: number | null;
  order_number: string | null;
  status: 'draft' | 'pending_approval' | 'approved' | 'posted' | 'partially_paid' | 'paid' | 'overdue' | 'cancelled';
  payment_status: 'unpaid' | 'partial' | 'paid' | 'overdue';
  reference: string | null;
  subject: string | null;
  notes: string | null;
  notes_ar: string | null;
  terms: string | null;
  terms_ar: string | null;
  currency_code: string;
  exchange_rate: string;
  payment_terms_days: number | null;
  subtotal: string;
  discount_amount: string;
  tax_amount: string;
  total: string;
  amount_paid: string;
  balance_due: string;
  journal_entry_id: number | null;
  cogs_journal_entry_id: number | null;
  items: SalesInvoiceItem[];
  created_by: number | null;
  created_by_name: string | null;
  approved_by: number | null;
  approved_at: string | null;
  approved_by_name: string | null;
  posted_by: number | null;
  posted_at: string | null;
  posted_by_name: string | null;
  cancelled_by: number | null;
  cancelled_at: string | null;
  cancellation_reason: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateSalesInvoice {
  date: string;
  due_date: string;
  customer_id: number;
  salesperson_id?: number | null;
  warehouse_id?: number | null;
  sales_order_id?: number | null;
  reference?: string | null;
  subject?: string | null;
  notes?: string | null;
  notes_ar?: string | null;
  terms?: string | null;
  terms_ar?: string | null;
  currency_code?: string;
  exchange_rate?: number;
  payment_terms_days?: number | null;
  items: SalesInvoiceItemPayload[];
}

export interface UpdateSalesInvoice extends Partial<CreateSalesInvoice> {}
