import { api } from '$lib/api';

export const invoicesReportsApi = {

    triggerPdfGeneration: async (invoiceId: number, companyId: number, invoiceType: string = 'mexican', currency: string = 'ORIGINAL') => {
        const params = new URLSearchParams({
            company_id: companyId.toString(),
            invoice_type: invoiceType,
            currency_code: currency
        });
        const endpoint = `/v1/a76/reports/importacion/facturas/${invoiceId}/download-async?${params.toString()}`;

        const res = await api.post(endpoint, {});
        if (res.error) throw new Error(res.error || 'Error al iniciar la generación');
        return res.data;
    },

    getTaskStatus: async (taskId: string) => {
        const endpoint = `/v1/a76/reports/importacion/facturas/tasks/${taskId}`;
        const res = await api.get(endpoint);
        if (res.error) throw new Error(res.error || 'Error al consultar estado');
        return res.data;
    },

    triggerPackingListGeneration: async (invoiceId: number, companyId: number) => {
        const params = new URLSearchParams({ company_id: companyId.toString() });
        const endpoint = `/v1/a76/reports/importacion/packing-lists/${invoiceId}/download-async?${params.toString()}`;

        const res = await api.post(endpoint, {});
        if (res.error) throw new Error(res.error || 'Error al iniciar la generación de Packing List');
        return res.data;
    },

    getPackingListTaskStatus: async (taskId: string) => {
        const endpoint = `/v1/a76/reports/importacion/packing-lists/tasks/${taskId}`;
        const res = await api.get(endpoint);
        if (res.error) throw new Error(res.error || 'Error al consultar estado de Packing List');
        return res.data;
    }
};