/* global React, Icons */
// ============================================================
//  AI helpers — call the built-in Claude helper
// ============================================================
const AI = {
  async run(prompt) {
    if (!window.claude || !window.claude.complete) {
      // graceful offline fallback
      await new Promise((r) => setTimeout(r, 900));
      return null;
    }
    try {
      const out = await window.claude.complete(prompt);
      return (out || "").trim();
    } catch (e) {
      return null;
    }
  },

  async rewriteBullet(text, role, mode = "improve") {
    const verb = {
      improve: "Rewrite this résumé bullet to be sharper and more impactful. Lead with a strong action verb, keep it to one line, and add a plausible quantified result if none exists.",
      shorten: "Shorten this résumé bullet to a tight, punchy single line without losing the key achievement.",
      quantify: "Rewrite this résumé bullet to emphasize measurable impact with a realistic metric (%, $, count, or time).",
    }[mode];
    const prompt = `${verb}\nRole context: ${role || "professional"}.\nReturn ONLY the rewritten bullet text, no quotes, no preamble.\n\nBullet: "${text}"`;
    const out = await AI.run(prompt);
    return out ? out.replace(/^["'\-\s]+|["']+$/g, "") : null;
  },

  async writeSummary(data) {
    const prompt = `Write a confident, first-person-free professional summary for a résumé (2-3 sentences, ~45 words). Person: ${data.name}, target role "${data.title}". Skills: ${(data.skills||[]).join(", ")}. Recent role: ${data.experience?.[0]?.role} at ${data.experience?.[0]?.company}. Return ONLY the summary text.`;
    return await AI.run(prompt);
  },

  async suggestSkills(data) {
    const prompt = `Given this résumé target role "${data.title}" and current skills [${(data.skills||[]).join(", ")}], suggest 6 additional relevant, in-demand skills. Return ONLY a comma-separated list, no numbering.`;
    const out = await AI.run(prompt);
    return out ? out.split(",").map((s) => s.trim()).filter(Boolean).slice(0, 6) : null;
  },
};

window.AI = AI;
