
const BASE_URL = import.meta.env.VITE_API_URL || '';

export const reportsTransmissionApi = {

    triggerGeneration: async (request: any) => {
        const endpoint = `${BASE_URL}/v1/a76/reports/exportacion/transmission/generate`;

        const token = localStorage.getItem('access_token');
        const response = await fetch(endpoint, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(request)
        });

                if (!response.ok) {
            let errorMessage = 'Error al iniciar la generación del archivo de transmisión';
            try {
                const errorData = await response.json();
                errorMessage = errorData.detail || errorData.message || errorMessage;
            } catch (e) {
                try {
                    const text = await response.text();
                    if (text) errorMessage = text;
                } catch (e2) {}
            }
            throw new Error(errorMessage);
        }
        return await response.json();
    },

    getTaskStatus: async (taskId: string) => {
        const endpoint = `${BASE_URL}/v1/a76/reports/exportacion/transmission/tasks/${taskId}`;

        const token = localStorage.getItem('access_token');
        const response = await fetch(endpoint, {
            method: 'GET',
            headers: { 'Authorization': `Bearer ${token}` }
        });

                if (!response.ok) {
            let errorMessage = 'Error al consultar estado de la transmisión';
            try {
                const errorData = await response.json();
                errorMessage = errorData.detail || errorData.message || errorMessage;
            } catch (e) {
                try {
                    const text = await response.text();
                    if (text) errorMessage = text;
                } catch (e2) {}
            }
            throw new Error(errorMessage);
        }
        return await response.json();
    },

    triggerTemporalGeneration: async (request: any) => {
        const endpoint = `${BASE_URL}/v1/a76/reports/importacion/transmission/temporal/generate`;

        const token = localStorage.getItem('access_token');
        const response = await fetch(endpoint, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(request)
        });

                if (!response.ok) {
            let errorMessage = 'Error al iniciar la generación temporal';
            try {
                const errorData = await response.json();
                errorMessage = errorData.detail || errorData.message || errorMessage;
            } catch (e) {
                try {
                    const text = await response.text();
                    if (text) errorMessage = text;
                } catch (e2) {}
            }
            throw new Error(errorMessage);
        }
        return await response.json();
    },

    getTemporalTaskStatus: async (taskId: string) => {
        const endpoint = `${BASE_URL}/v1/a76/reports/importacion/transmission/temporal/tasks/${taskId}`;

        const token = localStorage.getItem('access_token');
        const response = await fetch(endpoint, {
            method: 'GET',
            headers: { 'Authorization': `Bearer ${token}` }
        });

                if (!response.ok) {
            let errorMessage = 'Error al consultar estado de la transmisión temporal';
            try {
                const errorData = await response.json();
                errorMessage = errorData.detail || errorData.message || errorMessage;
            } catch (e) {
                try {
                    const text = await response.text();
                    if (text) errorMessage = text;
                } catch (e2) {}
            }
            throw new Error(errorMessage);
        }
        return await response.json();
    },

    triggerDefinitiveGeneration: async (request: any) => {
        const endpoint = `${BASE_URL}/v1/a76/reports/importacion/transmission/definitive/generate`;

        const token = localStorage.getItem('access_token');
        const response = await fetch(endpoint, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(request)
        });

                if (!response.ok) {
            let errorMessage = 'Error al iniciar la generación definitiva';
            try {
                const errorData = await response.json();
                errorMessage = errorData.detail || errorData.message || errorMessage;
            } catch (e) {
                try {
                    const text = await response.text();
                    if (text) errorMessage = text;
                } catch (e2) {}
            }
            throw new Error(errorMessage);
        }
        return await response.json();
    },

    getDefinitiveTaskStatus: async (taskId: string) => {
        const endpoint = `${BASE_URL}/v1/a76/reports/importacion/transmission/definitive/tasks/${taskId}`;

        const token = localStorage.getItem('access_token');
        const response = await fetch(endpoint, {
            method: 'GET',
            headers: { 'Authorization': `Bearer ${token}` }
        });

                if (!response.ok) {
            let errorMessage = 'Error al consultar estado de la transmisión definitiva';
            try {
                const errorData = await response.json();
                errorMessage = errorData.detail || errorData.message || errorMessage;
            } catch (e) {
                try {
                    const text = await response.text();
                    if (text) errorMessage = text;
                } catch (e2) {}
            }
            throw new Error(errorMessage);
        }
        return await response.json();
    }
};
