/**
 * Client HTTP per ar1-compiler (microservizio BFLOWS).
 * Il microservizio valida lo stesso JWT di GEPAFIN-BE (HS512 shared secret).
 *
 * Env var: REACT_APP_AR1_API_URL (es. http://78.46.41.91:18091)
 *
 * Pattern replicato 1:1 da rendicontazioneService.js.
 */
import { storeGet } from '../../../store';

const BASE_URL = process.env.REACT_APP_AR1_API_URL || '';

const buildHeaders = () => {
    const token = storeGet('getToken');
    const h = { 'Content-Type': 'application/json' };
    if (token) h['Authorization'] = `Bearer ${token}`;
    return h;
};

const buildHeadersMultipart = () => {
    const token = storeGet('getToken');
    const h = {};
    if (token) h['Authorization'] = `Bearer ${token}`;
    // niente Content-Type: fetch imposta boundary per multipart/form-data
    return h;
};

const handleResponse = async (response, onSuccess, onError) => {
    let body = null;
    try { body = await response.json(); } catch (e) { body = { detail: response.statusText }; }
    if (response.status >= 200 && response.status < 300) {
        if (onSuccess) onSuccess(body);
    } else {
        if (onError) onError({ status: response.status, ...body });
    }
};

const handleError = (err, onError) => {
    if (onError) onError({ status: 0, detail: err.message });
};

const Ar1Service = {
    // ---------- Status pubblico (per compliance modal al login) ----------
    getStatusForCompany(companyId, onSuccess, onError) {
        fetch(`${BASE_URL}/public/ar1-status/${companyId}`, {
            method: 'GET', mode: 'cors', headers: buildHeaders()
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    // ---------- CRUD form beneficiario ----------
    createDraft(companyId, variant, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms`, {
            method: 'POST', mode: 'cors', headers: buildHeaders(),
            body: JSON.stringify({ company_id: companyId, variant })
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    getForm(formId, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms/${formId}`, {
            method: 'GET', mode: 'cors', headers: buildHeaders()
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    listFormsForCompany(companyId, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms/company/${companyId}`, {
            method: 'GET', mode: 'cors', headers: buildHeaders()
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    updateQuadri(formId, quadriPatch, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms/${formId}/quadri`, {
            method: 'PUT', mode: 'cors', headers: buildHeaders(),
            body: JSON.stringify({ quadri: quadriPatch })
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    submitForSignature(formId, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms/${formId}/submit-for-signature`, {
            method: 'PUT', mode: 'cors', headers: buildHeaders()
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    deleteForm(formId, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms/${formId}`, {
            method: 'DELETE', mode: 'cors', headers: buildHeaders()
        })
            .then(r => {
                if (r.status === 204) {
                    if (onSuccess) onSuccess({});
                } else {
                    handleResponse(r, onSuccess, onError);
                }
            })
            .catch(e => handleError(e, onError));
    },

    // ---------- PDF ----------
    generatePdf(formId, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms/${formId}/generate-pdf`, {
            method: 'POST', mode: 'cors', headers: buildHeaders()
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    downloadPdfUnsigned(formId) {
        return fetch(`${BASE_URL}/api/ar1-forms/${formId}/pdf-unsigned`, {
            method: 'GET', mode: 'cors', headers: buildHeadersMultipart()
        }).then(r => {
            if (!r.ok) throw new Error(`HTTP ${r.status}`);
            return r.blob();
        });
    },

    downloadPdfSigned(formId) {
        return fetch(`${BASE_URL}/api/ar1-forms/${formId}/pdf-signed`, {
            method: 'GET', mode: 'cors', headers: buildHeadersMultipart()
        }).then(r => {
            if (!r.ok) throw new Error(`HTTP ${r.status}`);
            return r.blob();
        });
    },

    // ---------- Firma ----------
    uploadSignature(formId, fileObject, onSuccess, onError) {
        const formData = new FormData();
        formData.append('file', fileObject);
        fetch(`${BASE_URL}/api/ar1-forms/${formId}/upload-signature`, {
            method: 'POST', mode: 'cors', headers: buildHeadersMultipart(),
            body: formData
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    reVerifySignature(formId, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms/${formId}/verify`, {
            method: 'POST', mode: 'cors', headers: buildHeaders()
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },

    // ---------- Archive manuale (di solito automatico) ----------
    archiveToCompanyDocument(formId, onSuccess, onError) {
        fetch(`${BASE_URL}/api/ar1-forms/${formId}/archive-to-company-document`, {
            method: 'POST', mode: 'cors', headers: buildHeaders()
        })
            .then(r => handleResponse(r, onSuccess, onError))
            .catch(e => handleError(e, onError));
    },
};

export default Ar1Service;
