/**
 * Exemple d’utilisation :
 * Récupère les loyers avec un nombre de lignes défini
 * @param {number} rows - Nombre de lignes à récupérer
 */

const Revenue = {
    GetXRevenues: "x_revenues",
    GetLastRevenues: "last_revenue",
    GetAllRevenues: "all_revenues",
    GetAdditionalRevenues: "additional_revenues"
};

const Expense = {
    GetRightExpenses: "last_expenses"
}

var TotalRevenu = 0, TotalExpense = 0;

function loadRevenues() {
    return Promise.all([
        new Promise((resolve, reject) => {
            apiCall(Controller.Revenue, Revenue.GetXRevenues, { rows: 1 }, data => {
                const loyer = document.getElementById('salaire');
                const salaire = parseFloat(data[0]?.salary || 0);
                if (loyer) loyer.innerText = `${toPriceFormat(salaire)}€`;
                resolve(salaire);
            }, reject);
        }),
        new Promise((resolve, reject) => {
            apiCall(Controller.Revenue, Revenue.GetAdditionalRevenues, {}, data => {
                const additionalRevenues = document.getElementById('additionalRevenues');
                const totalAR = data.reduce((acc, item) => acc + (parseFloat(item.amount) || 0), 0);
                if (additionalRevenues) additionalRevenues.innerText = `${toPriceFormat(totalAR)}€`;
                resolve(totalAR);
            }, reject);
        })
    ]).then(([salary, additional]) => {
        TotalRevenu = salary + additional;
    });
}

function loadExpenses() {
    return new Promise((resolve, reject) => {
        apiCall(Controller.Expense, Expense.GetRightExpenses, {}, data => {
            const loyer = document.getElementById('loyer');
            const trash = document.getElementById('trash');
            const electricity = document.getElementById('electricity');
            const insurance = document.getElementById('insurance');
            const wifi = document.getElementById('wifi');
            const groceries = document.getElementById('groceries');
            const additionalSourcesExpense = document.getElementById('additionalSourcesExpense');
            const additionalExpensesSub = document.getElementById('additionalExpensesSub');
            const saving = document.getElementById('saving');

            if (loyer) loyer.innerText = `${toPriceFormat(data.rent)}€`;
            if (trash) trash.innerText = `${toPriceFormat(data.trash)}€`;
            if (insurance) insurance.innerText = `${toPriceFormat(data.insurance)}€`;
            if (electricity) electricity.innerText = `${toPriceFormat(data.electricity)}€`;
            if (wifi) wifi.innerText = `${toPriceFormat(data.wifi)}€`;
            if (groceries) groceries.innerText = `${toPriceFormat(data.groceries)}€`;
            if (saving) saving.innerText = `${toPriceFormat(data.saving)}€`;

            if (additionalSourcesExpense) {
                const totalAR = data.additionalSourcesExpense.reduce((acc, item) => acc + (parseFloat(item.amount) || 0), 0);
                additionalSourcesExpense.innerText = `${toPriceFormat(totalAR)}€`;
            }

            if (additionalExpensesSub) {
                const totalAR = data.additionalSourcesSub.reduce((acc, item) => acc + (parseFloat(item.amount) || 0), 0);
                additionalExpensesSub.innerText = `${toPriceFormat(totalAR)}€`;
            }

            TotalExpense = data.total;
            resolve();
        }, reject);
    });
}




// Lance automatiquement l’appel au chargement de la page
document.addEventListener('DOMContentLoaded', () => {
    const result = document.getElementById('result');

    Promise.all([loadExpenses(), loadRevenues()])
        .then(() => {
            if (result) result.innerText = `${toPriceFormat(TotalRevenu - TotalExpense)}€`;
        })
        .catch(error => {
            console.error("❌ Erreur de chargement :", error);
            if (result) result.innerText = "Erreur lors du calcul";
        });
});

