// sense-check-data.jsx
// Questions and live API functions.
// Exported via window.SenseCheckData — consumed by sense-check-app.jsx.

// ── Questions ────────────────────────────────────────────────────────────────
// Sourced from /data/questions.json. Embedded here for the no-build CDN setup.

const QUESTIONS = [
  {
    id: "q1_postponed_no",
    title: "The Postponed \u201cNo\u201d",
    desc: "What is the \u201cno\u201d you are currently postponing in your practice \u2014 a client, an offering, or a routine you keep saying \u201cyes\u201d to \u2014 simply because you don\u2019t feel you have the bandwidth or leverage to replace it?",
    placeholder: "e.g. \u201cA long-standing client whose scope has drifted into work I no longer want to do. I keep renewing because replacing the revenue feels harder than tolerating it.\u201d",
  },
  {
    id: "q2_delegation_friction",
    title: "The Friction of Delegation",
    desc: "Think of the last time you tried to hand a task over to a tool or an assistant, but ended up stepping in to finish it yourself. Was the output actually unusable, or did the loss of your usual control over the process just feel too uncomfortable to tolerate?",
    placeholder: "e.g. \u201cAsked Claude to draft client session notes. Output was 80% fine but I rewrote the whole thing anyway \u2014 I couldn\u2019t trust that my voice wasn\u2019t lost.\u201d",
  },
  {
    id: "q3_existential_threat",
    title: "The Existential Threat",
    desc: "As a professional, your clients pay you for your unique expertise and judgment. If an automated system could suddenly handle 80% of the tangible deliverables you currently provide, what is the quiet, uncomfortable worry about your remaining value?",
    placeholder: "e.g. \u201cThat half of what I charge for is the polished deliverable, not the judgment behind it. If the deliverable is free, I don\u2019t know what I\u2019m actually charging for.\u201d",
  },
  {
    id: "q4_energy_drain",
    title: "The Energy Drain",
    desc: "Look at your calendar for the upcoming week. What is one recurring task you\u2019ve silently agreed to tolerate, even though it drains the exact energy you need to do the work your clients actually pay you for?",
    placeholder: "e.g. \u201cInvoicing and chasing payment. Takes two hours, drains the same focus I need for client prep, and I\u2019ve avoided systematising it for three years.\u201d",
  },
  {
    id: "q5_hidden_payoff",
    title: "The Hidden Payoff",
    desc: "We often hold onto frustrating ways of working because they secretly serve us. What is the hidden payoff you get for continuing to do things the \u201chard way,\u201d rather than risking the disruption of changing your workflow?",
    placeholder: "e.g. \u201cDoing it manually lets me feel like I\u2019m \u2018really\u2019 earning the fee. If I automated it, I\u2019d have to justify pricing on value instead of effort.\u201d",
  },
  {
    id: "q6_reclaimed_hour",
    title: "The Reclaimed Hour",
    desc: "Fast forward 90 days. If you successfully integrated a new way of working, what is one specific, tedious task that is completely gone from your desk, and exactly what are you doing with that reclaimed hour instead?",
    placeholder: "e.g. \u201cSession notes are drafted automatically from recordings. I use the reclaimed hour for a standing Thursday walk with my wife \u2014 no laptop.\u201d",
  },
];

// ── API functions ─────────────────────────────────────────────────────────────
// All call /api/sense-check with a step discriminator.

async function ackAnswer({ questionNumber, questionTitle, currentAnswer, priorAnswers, probesRemaining }) {
  const res = await fetch('/api/sense-check', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      step: 'acknowledge',
      questionNumber,
      questionTitle,
      currentAnswer,
      priorAnswers,     // [{ title, answer }] for prior questions only
      probesRemaining,
    }),
  });
  if (!res.ok) throw new Error((await res.json()).error || 'Acknowledgement failed.');
  return res.json(); // { acknowledgement: string, probe: string | null }
}

async function generateReadback(answers) {
  // answers: [{ id, title, answer, probeQuestion, probeAnswer }]
  const res = await fetch('/api/sense-check', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ step: 'read_back', answers }),
  });
  if (!res.ok) throw new Error((await res.json()).error || 'Read-back failed.');
  const data = await res.json();
  return data.read_back_paragraph; // plain string — one paragraph
}

async function generateReport({ answers, readBackParagraph, correctionNote }) {
  const res = await fetch('/api/sense-check', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      step: 'report',
      answers,
      readBackParagraph,
      correctionNote: correctionNote || null,
    }),
  });
  if (!res.ok) throw new Error((await res.json()).error || 'Report generation failed.');
  return res.json();
  // Returns: { report_version, sections: { the_read, hype_filter, real_opportunity,
  //   provocation, next_step: { headline, detail, timeframe }, i_dont_know_close },
  //   meta: { tone_cue, primary_mood_signal, confidence } }
}

async function submitEmail({ email, marketingConsent, reportSnapshot }) {
  const res = await fetch('/api/sense-check', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      step: 'email',
      email,
      marketingConsent,
      reportSnapshot, // full report object from generateReport()
    }),
  });
  if (!res.ok) throw new Error((await res.json()).error || 'Email send failed.');
  return res.json(); // { success: true }
}

// ── Export ────────────────────────────────────────────────────────────────────
window.SenseCheckData = { QUESTIONS, ackAnswer, generateReadback, generateReport, submitEmail };
