/* global React */
/* ============================================================
   The Executive Authority Signal Audit
   ============================================================
   Not a quiz. A diagnostic with a private reading at the end.

   Each question presents a moment under pressure and offers four
   responses. Each response maps to one of four "signatures" of
   authority leakage. The result is the dominant signature plus
   the secondary, with reflective copy — not a score.

   The four signatures (kept private to the audit):
     E — Explanation:  authority leaks through over-justification
     C — Composure:    authority leaks through pace + body
     D — Deflection:   authority leaks through redirecting away from self
     R — Recovery:     authority leaks through what happens after
   ============================================================ */

const AUDIT_QUESTIONS = [
  {
    id: 'q1',
    eyebrow: 'Scrutiny',
    prompt: 'A board member you respect pushes back on your reading of the quarter, with weight in their voice. Within the first five seconds, your instinct is to:',
    options: [
      { sig: 'E', label: 'Add another reason. Then another. The case I made was apparently insufficient.' },
      { sig: 'C', label: 'Slow down. Hold the room. Say less, not more.' },
      { sig: 'D', label: 'Move the conversation toward the data, the team, the framework.' },
      { sig: 'R', label: 'Stay measured in the room. Replay the moment later, alone.' },
    ],
  },
  {
    id: 'q2',
    eyebrow: 'Over-explanation',
    prompt: 'In a senior review, the data is weaker than expected. The first instinct is to:',
    options: [
      { sig: 'E', label: 'Frame the context fully before anyone misreads it.' },
      { sig: 'C', label: 'Sit with it. Let the silence do part of the work.' },
      { sig: 'D', label: 'Name what changed in the environment, in the market, in the inputs.' },
      { sig: 'R', label: 'State the number. Speak about it differently in the corridor afterward.' },
    ],
  },
  {
    id: 'q3',
    eyebrow: 'Authority',
    prompt: 'Where does your authority leak most reliably?',
    options: [
      { sig: 'E', label: 'I explain decisions that did not need defending.' },
      { sig: 'C', label: 'My pace gives me away under pressure. Voice. Speed. Breath.' },
      { sig: 'D', label: 'I become the translator. I stop being the one whose view shapes the room.' },
      { sig: 'R', label: 'The moment passes intact. The cost shows up later, privately.' },
    ],
  },
  {
    id: 'q4',
    eyebrow: 'Reconsidered',
    prompt: 'A decision you made cleanly two weeks ago is being revisited by a senior peer. You:',
    options: [
      { sig: 'E', label: 'Walk through the reasoning again, carefully, to bring them onto it.' },
      { sig: 'C', label: 'Hold the decision. Note what I would do the same. Say it once.' },
      { sig: 'D', label: 'Reopen the inputs. The room can re-examine alongside me.' },
      { sig: 'R', label: 'Defend it in the meeting. Question it to myself for the rest of the week.' },
    ],
  },
  {
    id: 'q5',
    eyebrow: 'Pattern',
    prompt: 'The pattern that repeats most often when a room is harder than expected:',
    options: [
      { sig: 'E', label: 'I leave the meeting wishing I had said less.' },
      { sig: 'C', label: 'I leave the meeting wishing I had been slower.' },
      { sig: 'D', label: 'I leave the meeting wishing I had centred my own view.' },
      { sig: 'R', label: 'I leave the meeting fine. I become the harshest reader of it that night.' },
    ],
  },
  {
    id: 'q6',
    eyebrow: 'Consequence',
    prompt: 'When the stakes are personal as well as professional, your composure:',
    options: [
      { sig: 'E', label: 'Holds. My language gets more careful, sometimes too careful.' },
      { sig: 'C', label: 'Holds in body. Frays in tempo. Something in the rhythm goes thin.' },
      { sig: 'D', label: 'Holds visibly. I find a more comfortable angle on the conversation.' },
      { sig: 'R', label: 'Holds publicly. Costs me sleep, costs me the next morning.' },
    ],
  },
  {
    id: 'q7',
    eyebrow: 'The tell',
    prompt: 'The first physical or vocal sign that something is slipping:',
    options: [
      { sig: 'E', label: 'A second clause. Then a third. My sentences grow.' },
      { sig: 'C', label: 'Tempo. I speed up. My voice rises a half-step.' },
      { sig: 'D', label: 'My eye contact widens. I include more people in the answer.' },
      { sig: 'R', label: 'Nothing visible. The cost is private and delayed.' },
    ],
  },
  {
    id: 'q8',
    eyebrow: 'Performance',
    prompt: 'Honest answer. When you walk into a high-consequence room, you are:',
    options: [
      { sig: 'E', label: 'Preparing the case. I want the room to follow the logic.' },
      { sig: 'C', label: 'Settling the body. I want the room to read calm before content.' },
      { sig: 'D', label: 'Reading the room. I want to enter on its frequency.' },
      { sig: 'R', label: 'Already past it. I am rehearsing how I will feel about it on Friday.' },
    ],
  },
  {
    id: 'q9',
    eyebrow: 'Recovery',
    prompt: 'After a meeting where you lost the room, your private response is to:',
    options: [
      { sig: 'E', label: 'Write a long note clarifying what I meant. I do not always send it.' },
      { sig: 'C', label: 'Walk. Reset. Decide what I will hold differently next time.' },
      { sig: 'D', label: 'Ask three people what they saw. Triangulate.' },
      { sig: 'R', label: 'Carry it. For a week. Sometimes longer.' },
    ],
  },
  {
    id: 'q10',
    eyebrow: 'What holds',
    prompt: 'When everything else in the room is loud, what holds firm in you?',
    options: [
      { sig: 'E', label: 'The argument. I trust the structure of the case.' },
      { sig: 'C', label: 'The body. I trust posture, breath, pace.' },
      { sig: 'D', label: 'The relationships. I trust the room\u2019s read of me over years.' },
      { sig: 'R', label: 'The privacy of it. I trust that I will not show what it cost.' },
    ],
  },
];

/* ============================================================
   Signatures — the reading the audit produces.
   ============================================================ */
const SIGNATURES = {
  E: {
    code: 'E',
    name: 'Authority leaks through explanation.',
    short: 'The room receives competence. It does not receive conviction.',
    reading: [
      'Your reasoning is unusually strong. That is also where it leaks.',
      'Under pressure, your instinct is to add. Another clause. Another reason. Another framing. The argument compounds. The signal does not.',
      'The room concludes you are capable. The room does not always conclude you are settled. Those are different impressions, and senior rooms can feel the difference within seconds.',
    ],
    pattern: 'Add → clarify → over-include → re-explain.',
    cost: 'You are heard as the smartest person in the room, and the least decided.',
    work: 'The work is not to think less. The work is to stop performing the thinking.',
  },
  C: {
    code: 'C',
    name: 'Authority leaks through pace.',
    short: 'The case is sound. The body is faster than the case.',
    reading: [
      'Your content is steady. Your tempo is not.',
      'Under pressure, something in pace, breath, or pitch moves before you do. The room reads it before it reads the words. The reading happens unconsciously, in the listener\u2019s body, before they can articulate it.',
      'You may not notice the slip in the moment. The room does. Authority, once it accelerates, has to be earned back deliberately.',
    ],
    pattern: 'Accelerate → rise in pitch → narrow breath → carry through.',
    cost: 'The substance is correct. The signal arrives flickering.',
    work: 'Composure is not stillness. It is a deliberate tempo under load.',
  },
  D: {
    code: 'D',
    name: 'Authority leaks through deflection.',
    short: 'You move the conversation away from your own view.',
    reading: [
      'You are skilled at translating between people. That skill, sharpened, becomes a hiding place.',
      'Under pressure, your instinct is to center the room, the data, the team, the framework. Anything that is not you-as-the-decider. You exit the seat without leaving it.',
      'The cost is identity, not capability. The room ends the meeting unsure whose view shaped what happened. Often, including yourself.',
    ],
    pattern: 'Translate → frame → re-center → exit the seat.',
    cost: 'You preserve relationship. You forfeit authorship.',
    work: 'Stay in the seat. The room can take your view. It cannot take your absence from it.',
  },
  R: {
    code: 'R',
    name: 'Authority leaks after the room.',
    short: 'Nothing slips publicly. The cost is paid privately and late.',
    reading: [
      'In the moment, you hold. That is real, and it is rare.',
      'The leak happens after. You replay the meeting. You revise the decision in private. You carry the room with you for hours, sometimes days. Sometimes the next meeting inherits the unfinished business of the last one.',
      'External composure stays intact. Internal composure compounds debt. The pattern is invisible to peers and visible only to you, your sleep, and the people closest to you.',
    ],
    pattern: 'Hold publicly → replay privately → carry → arrive incomplete.',
    cost: 'You stay credible to the room. You become less available to yourself.',
    work: 'The room is over when the room is over. Reclaim the night.',
  },
};

/* ============================================================
   Scoring — count signatures, return primary + secondary
   ============================================================ */
function scoreAudit(answers) {
  const counts = { E: 0, C: 0, D: 0, R: 0 };
  Object.values(answers).forEach((sig) => { if (counts[sig] != null) counts[sig] += 1; });
  const ranked = Object.entries(counts).sort((a, b) => b[1] - a[1]);
  return {
    counts,
    primary: ranked[0][0],
    secondary: ranked[1][1] > 0 ? ranked[1][0] : null,
    total: Object.keys(answers).length,
  };
}

/* ============================================================
   Pacing durations from tweak
   ============================================================ */
function pacingMs(pacing) {
  switch (pacing) {
    case 'quick':    return { transition: 320, beforeAdvance: 220 };
    case 'slow':     return { transition: 900, beforeAdvance: 600 };
    case 'measured':
    default:         return { transition: 600, beforeAdvance: 380 };
  }
}

Object.assign(window, { AUDIT_QUESTIONS, SIGNATURES, scoreAudit, pacingMs });
