const form = document.querySelector("#upload-form");
const result = document.querySelector("#result");
const summary = document.querySelector("#summary");
const skills = document.querySelector("#skills");
const mdLink = document.querySelector("#download-md");
const progressBar = document.querySelector("#analysis-progress");
const statusText = document.querySelector("#analysis-status");
const button = form.querySelector("button");

async function pollTask(statusUrl) {
  while (true) {
    const response = await fetch(statusUrl);
    const payload = await response.json();

    progressBar.style.width = `${payload.progress || 0}%`;
    statusText.textContent = payload.message || "分析中";

    if (payload.status === "completed") {
      return payload;
    }

    if (payload.status === "error") {
      throw new Error(payload.error || "分析失败");
    }

    await new Promise((resolve) => setTimeout(resolve, 1000));
  }
}

form.addEventListener("submit", async (event) => {
  event.preventDefault();
  button.disabled = true;
  button.textContent = "分析中...";
  result.hidden = true;
  progressBar.style.width = "0%";
  statusText.textContent = "任务提交中...";

  const data = new FormData(form);
  if (!data.has("use_model")) {
    data.set("use_model", "false");
  }

  try {
    const response = await fetch("/analyze", {
      method: "POST",
      body: data,
    });
    const payload = await response.json();
    if (!response.ok) {
      throw new Error(payload.detail || "分析失败");
    }

    result.hidden = false;
    const task = await pollTask(payload.status_url);
    summary.textContent = task.summary;
    skills.innerHTML = "";
    task.matched_skills.forEach((name) => {
      const item = document.createElement("span");
      item.textContent = name;
      skills.appendChild(item);
    });
    mdLink.href = task.downloads.markdown;
  } catch (error) {
    summary.textContent = error.message;
    skills.innerHTML = "";
    result.hidden = false;
    statusText.textContent = "分析失败";
  } finally {
    button.disabled = false;
    button.textContent = "开始分析";
  }
});
