/* =============================== */
/* DemoBank Currency Module         */
/* =============================== */

document.addEventListener("DOMContentLoaded", function () {

    const fromCurrency = document.querySelector("#from_currency");
    const toCurrency = document.querySelector("#to_currency");
    const amountInput = document.querySelector("#amount");
    const resultBox = document.querySelector("#conversion_result");
    const swapBtn = document.querySelector("#swap_currency");
    const convertBtn = document.querySelector("#convert_btn");

    // ===============================
    // FORMAT NUMBER
    // ===============================
    function formatNumber(num) {
        return parseFloat(num).toFixed(2);
    }

    // ===============================
    // BASIC FRONTEND CONVERSION (FALLBACK)
    // NOTE: Replace with backend API for real rates
    // ===============================
    async function convertCurrency() {

        if (!fromCurrency || !toCurrency || !amountInput) return;

        const from = fromCurrency.value;
        const to = toCurrency.value;
        const amount = parseFloat(amountInput.value);

        if (!from || !to || isNaN(amount)) {
            alert("Please select currencies and enter a valid amount.");
            return;
        }

        // Placeholder rates (fallback only)
        const fakeRates = {
            USD: { EUR: 0.92, GBP: 0.78, NGN: 1500 },
            EUR: { USD: 1.09, GBP: 0.85, NGN: 1600 },
            GBP: { USD: 1.28, EUR: 1.17, NGN: 1800 },
            NGN: { USD: 0.00067, EUR: 0.00063, GBP: 0.00056 }
        };

        let rate = 1;

        if (fakeRates[from] && fakeRates[from][to]) {
            rate = fakeRates[from][to];
        }

        const converted = amount * rate;

        if (resultBox) {
            resultBox.innerHTML = `
                <strong>Converted Amount:</strong><br>
                ${formatNumber(amount)} ${from} = 
                <span style="color:#004080;font-weight:bold;">
                    ${formatNumber(converted)} ${to}
                </span>
            `;
        }
    }

    // ===============================
    // CONVERT BUTTON
    // ===============================
    if (convertBtn) {
        convertBtn.addEventListener("click", function (e) {
            e.preventDefault();
            convertCurrency();
        });
    }

    // ===============================
    // LIVE AUTO CONVERT (OPTIONAL UX)
    // ===============================
    if (amountInput) {
        amountInput.addEventListener("input", function () {
            if (amountInput.value.length > 0) {
                convertCurrency();
            }
        });
    }

    // ===============================
    // SWAP CURRENCIES
    // ===============================
    if (swapBtn) {
        swapBtn.addEventListener("click", function () {

            if (!fromCurrency || !toCurrency) return;

            const temp = fromCurrency.value;
            fromCurrency.value = toCurrency.value;
            toCurrency.value = temp;

            convertCurrency();
        });
    }

    // ===============================
    // AUTO FORMAT AMOUNT INPUT
    // ===============================
    if (amountInput) {
        amountInput.addEventListener("blur", function () {
            if (amountInput.value) {
                amountInput.value = parseFloat(amountInput.value).toFixed(2);
            }
        });
    }

});