document.getElementById('uploadForm').addEventListener('submit', async (e) => {
  e.preventDefault();
  const fileEl = document.getElementById('csvfile');
  if (!fileEl.files.length) return;
  const fd = new FormData();
  fd.append('csvfile', fileEl.files[0]);
  const resEl = document.getElementById('uploadResult');
  resEl.innerText = 'Enviando...';
  try {
    const resp = await fetch('/upload', { method: 'POST', body: fd });
    const data = await resp.json();
    if (data.jobId) {
      // show progress bar
      document.getElementById('progressWrap').style.display = 'block';
      pollJob(data.jobId);
      resEl.innerText = `Consultando viabilidade...`;
    } else if (data.error) {
      resEl.innerText = 'Erro: ' + data.error;
    }
  } catch (e) {
    resEl.innerText = 'Erro no upload';
  }
});

async function pollJob(jobId) {
  const resEl = document.getElementById('uploadResult');
  const bar = document.getElementById('progressBar');
  try {
    const resp = await fetch(`/status/${jobId}`);
    const j = await resp.json();
    if (j.total && j.total > 0) {
      const pct = Math.round((j.processed / j.total) * 100);
      bar.style.width = pct + '%';
      bar.innerText = `${pct}%`;
    }
    if (j.status === 'done') {
      bar.style.width = '100%';
      bar.innerText = '100%';
      resEl.innerHTML = `Concluído. <a href="${j.download}">Baixar CSV processado</a>`;
      return;
    }
    if (j.status === 'error') {
      resEl.innerText = 'Erro no processamento: ' + j.error;
      return;
    }
    // ainda processando
    setTimeout(() => pollJob(jobId), 1000);
  } catch (e) {
    resEl.innerText = 'Erro ao consultar status do job';
  }
}

document.getElementById('btnConsultaCep').addEventListener('click', async () => {
  const cep = document.getElementById('cep').value;
  const numero = document.getElementById('numero').value;
  const el = document.getElementById('consultaResult');
  el.innerText = 'Consultando...';
  try {
    const resp = await fetch(`/consulta-cep?cep=${encodeURIComponent(cep)}&numero=${encodeURIComponent(numero)}`);
    const data = await resp.json();
    if (data.distancia) {
      el.innerText = `Endereço: ${data.endereco}\nLat: ${data.latitude} Lon: ${data.longitude}\nDistância: ${data.distancia}`;
    } else if (data.error) {
      el.innerText = 'Erro: ' + data.error;
    }
  } catch (e) {
    el.innerText = 'Erro na consulta';
  }
});
