export type LedgerType = 'entree' | 'sortie';
export type LedgerCurrency = 'TND' | 'EUR' | 'USD';
export type LedgerSource = 'manual' | 'recovery' | 'expense' | 'receipt' | 'allocation';

export interface LedgerRow {
    id: number;
    date: string; // YYYY-MM-DD
    created_at: string | null; // ISO timestamp of creation, used to show HH:mm in the date cell
    designation: string;
    type: LedgerType;
    amount: number;
    currency: LedgerCurrency;
    amount_tnd: number;
    fx_rate: number | null;
    solde: number | null;     // null for soft-deleted rows
    deleted_at: string | null; // ISO timestamp when soft-deleted, null otherwise
    source: LedgerSource;
    recovery_id: number | null;
    expense_id: number | null;
    receipt_id: number | null;
    paired_entry_id: number | null;     // present on team allocations (paired entries)
    owner_user_id: number | null;
    owner_name: string | null;          // owner full name (admin view shows it)
    facture_pending?: boolean;          // expense row whose facture hasn't been uploaded yet
    allocation_status?: 'awaiting_signature' | 'pending' | 'reported' | 'approved' | 'closed' | 'rejected' | null;
    alloc_can_report?: boolean;         // current viewer is the receiver and must report
    alloc_can_approve?: boolean;        // current viewer is the giver and must approve
    alloc_can_return?: boolean;         // current viewer (giver) can confirm the return
}

export interface LedgerFilters {
    month: string | null;   // YYYY-MM
    year: string | null;    // YYYY (mutually exclusive with month)
    from: string | null;
    to: string | null;
    search: string;
    type: 'all' | LedgerType;
    status: 'active' | 'deleted' | 'all';
    source: 'all' | LedgerSource;
    sort?: 'asc' | 'desc';  // date sort direction; default 'desc' (newest first)
    owner?: number | null;  // admin-only: filter to one user's fond_de_caisse
}

export interface LedgerSummary {
    total_entrees: number;
    total_sorties: number;
    current_solde: number;
}

export type FxRates = Record<LedgerCurrency, number>;
