document.addEventListener("DOMContentLoaded", function(){
    const providerTabs = document.querySelectorAll(".tp-tab");
    const sigmaPane = document.getElementById("paneSigma");
    const fastplayPane = document.getElementById("paneFastplay");
    const hiddenApiUrl = document.getElementById("api_url_hidden");
    const sigmaInput = sigmaPane.querySelector('input[name="api_url"]');
    const enabledSwitch = document.getElementById("enabledSwitch");
    const enabledLabel = document.querySelector('label[for="enabledSwitch"]');
    const fastplayAdultos = document.getElementById("fastplayAdultos");
    const bouquetBox = document.getElementById("bouquetBox");
    const bouquetPreview = document.getElementById("bouquetPreview");
    const apiBouquetHidden = document.getElementById("api_bouquet_hidden");

    function getSelectedProvider(){
        const checked = document.querySelector('input[name="provider"]:checked');
        return checked ? checked.value : "sigma";
    }

    function getFastplayBouquet(){
        return fastplayAdultos && fastplayAdultos.checked ? "1,2,3,4" : "1,2,3";
    }

    function updateFastplayBouquetUI(){
        const bouquet = getFastplayBouquet();

        if (bouquetBox) {
            bouquetBox.textContent = bouquet;
        }

        if (bouquetPreview) {
            bouquetPreview.innerHTML = 'Bouquet atual: <strong>' + bouquet + '</strong>';
        }

        if (apiBouquetHidden) {
            apiBouquetHidden.value = bouquet;
        }
    }

    function applyProviderUI(provider){
        providerTabs.forEach(tab => {
            tab.classList.toggle("active", tab.dataset.provider === provider);
        });

        sigmaPane.classList.toggle("active", provider === "sigma");
        fastplayPane.classList.toggle("active", provider === "fastplay");

        if (provider === "fastplay") {
            hiddenApiUrl.value = "https://api.painelcliente.com";
            updateFastplayBouquetUI();
        } else {
            hiddenApiUrl.value = sigmaInput.value.trim();
        }
    }

    providerTabs.forEach(tab => {
        tab.addEventListener("click", function(){
            const provider = tab.dataset.provider;
            const radio = tab.querySelector('input[type="radio"]');
            if (radio) {
                radio.checked = true;
            }
            applyProviderUI(provider);
        });
    });

    sigmaInput.addEventListener("input", function(){
        if (getSelectedProvider() === "sigma") {
            hiddenApiUrl.value = sigmaInput.value.trim();
        }
    });

    if (fastplayAdultos) {
        fastplayAdultos.addEventListener("change", function(){
            updateFastplayBouquetUI();
        });
    }

    enabledSwitch.addEventListener("change", function(){
        enabledLabel.textContent = enabledSwitch.checked ? "Ativo" : "Inativo";
    });

    document.getElementById("formConfigTeste").addEventListener("submit", function(){
        const provider = getSelectedProvider();

        if (provider === "fastplay") {
            hiddenApiUrl.value = "https://api.painelcliente.com";
            updateFastplayBouquetUI();
        } else {
            hiddenApiUrl.value = sigmaInput.value.trim();
        }
    });

    updateFastplayBouquetUI();
    applyProviderUI(getSelectedProvider());
});
