const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');

async function processFile(file) {
    const statusText = document.getElementById('status-text');
    document.getElementById('status-area').classList.remove('d-none');

    const adapterSelect = document.getElementById('adapter-select');
    const adapterId = adapterSelect.value;

    let adapterName = "";
    if (adapterSelect.selectedIndex > 0) {
        adapterName = adapterSelect.options[adapterSelect.selectedIndex].getAttribute('data-name');
    }

    try {
        let response;

        if (file.name.toLowerCase().endsWith('.json')) {
            if (!adapterName) {
                statusText.innerText = "Unknown JSON format. Redirecting to Adapter Builder...";
                setTimeout(() => {
                    window.location.href = `/admin/adapters/new?filename=${encodeURIComponent(file.name)}`;
                }, 1200);
                return;
            }

            const rawText = await file.text();
            response = await fetch(`/api/ingest/${encodeURIComponent(adapterName)}`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: rawText
            });
        }
        else {
            let formData = new FormData();
            formData.append('file', file);
            if (adapterId) {
                formData.append('adapter_id', adapterId);
            }

            response = await fetch('/api/ingest/csv', { method: 'POST', body: formData });
        }

        if (!response.ok) {
            if (response.status === 404) {
                statusText.innerText = "Format not recognized. Redirecting to Adapter Builder...";
                setTimeout(() => {
                    window.location.href = `/admin/adapters/new?filename=${encodeURIComponent(file.name)}`;
                }, 1200);
            } else {
                const errText = await response.text();
                throw new Error(errText);
            }
        } else {
            statusText.innerText = "Yeehaw! Tickets corralled successfully.";
            setTimeout(() => window.location.href = "/dashboard", 800);
        }
    } catch (err) {
        statusText.innerText = "Stampede! Error: " + err.message;
    }
}

dropZone.onclick = () => fileInput.click();
fileInput.onchange = (e) => processFile(e.target.files[0]);
dropZone.ondragover = (e) => { e.preventDefault(); dropZone.style.background = "#e1f5fe"; };
dropZone.ondragleave = () => dropZone.style.background = "#f8f9fa";
dropZone.ondrop = (e) => { e.preventDefault(); processFile(e.dataTransfer.files[0]); };