/**
 * API Client for Saldos Temporales Report
 */
import { api } from '$lib/api';

// ===== TYPES =====

export interface SaldosFilter {
    // Range / Period
    range_type: string;          // pedimento | payment_date | invoice_date | parts | classes
    start_date?: string | null;  // For pedimento/part/class identifiers
    end_date?: string | null;    // For pedimento/part/class identifiers
    date_start?: string | null;  // Explicit start date filter
    date_end?: string | null;    // Explicit end date filter

    // Column 2 – Filtros e Identificadores
    currency: string;            // foreign | national
    report_type: string;         // normal | detailed | with_download
    exchange_rate: string;       // invoice_date | payment_date
    omit_low_balance: boolean;
    balance_type: string;        // normal | repair | both
    level: string;               // partida | subpartida | all

    // Column 3 – Configuración Final
    end_date_as_cutoff: boolean;
    show_pending_series: boolean;
    include_asset_tag_images: boolean;
    use_large_asset_tag_icons: boolean;
    send_email: boolean;
    julian_date: boolean;
    include_regla_octava: boolean;
    shelter: boolean;

    // Print options
    print_part: boolean;
    print_class: boolean;

    // Optional identifiers populated from catalog selectors
    provider?: string | null;
    buyer?: string | null;
    asset_class?: string | null;
    asset_type?: string | null;
    location?: string | null;
    pedimento_code?: string | null;
}

// ===== API METHODS =====

export const saldosReportApi = {
    /** Trigger async CSV generation; returns task_id */
    generateReportAsync: (filters: SaldosFilter, companyId: number) =>
        api.post<{ task_id: string }>(`/v1/a76/reports/movements/saldos/generate?company_id=${companyId}`, filters),

    /** Poll task status */
    getTaskStatus: (taskId: string) =>
        api.get<{ task_id: string; status: string; result?: any; meta?: any }>(
            `/v1/a76/reports/movements/saldos/task/${taskId}`
        )
};
