/* global React, TOKENS, FONTS, Icon, hexA */
// Eighty4 · persona-driven example content. Four real people running an 84-day cycle.
// Adapted from the app's example personas, copy tightened.

const PERSONAS = [
  {
    key: "entrepreneur", title: "Entrepreneur", icon: "briefcase",
    blurb: "Pipeline, proposals, and protected evenings",
    cycle: "Advisory growth cycle",
    vision: "By week 12 I've got $60k in qualified pipeline, two signed advisory clients, a clean delivery cadence, and a steady running base.",
    goals: [
      { title: "Build a $60k advisory pipeline with 2 signed clients", pct: 40, ratio: "2/5", next: "Book 12 discovery calls with qualified operators" },
      { title: "Hit 25 miles a week for 8 of the 12 weeks", pct: 25, ratio: "1/4", next: "Build the long run from 6 to 9 miles" },
      { title: "Protect 40 phone-free evenings after 8pm", pct: 75, ratio: "3/4", next: "Review the streak each Sunday with my spouse" },
    ],
    weekPriority: "Run the first discovery calls and qualify the pipeline",
    weekSupport: [
      { t: "Hold 4 discovery calls", s: "doing" },
      { t: "Write one-page notes after each call", s: "open" },
      { t: "Long run on Saturday", s: "open" },
      { t: "Draft the proposal outline", s: "open" },
    ],
    mission: "Finish the proposal for Acme Operations",
    secondary: [
      { t: "Ask two referral partners for intros", done: false },
      { t: "Easy run, 5 miles", done: true },
      { t: "Pick tonight's book before 8pm", done: false },
    ],
    additional: [
      { t: "Reply to the accountant", done: false },
      { t: "Buy printer paper", done: false },
      { t: "Confirm Friday dinner", done: true },
    ],
    schedule: [
      { time: "7:00a", label: "Easy run, 5 miles" },
      { time: "9:00a", label: "Deep work: Acme proposal", now: true },
      { time: "1:00p", label: "Referral partner calls" },
      { time: "7:45p", label: "Shutdown, phone away" },
    ],
    note: "Today is about turning the best conversation into a concrete next step. Finish the Acme proposal, ask for two intros, keep the run easy.",
    captures: [
      { time: "8:42a", text: "Acme's real pain: dropped handoffs between sales, onboarding, and delivery." },
      { time: "11:15a", text: "Ask Jordan and Priya for one operator intro each." },
    ],
    habits: [
      { name: "Plan tomorrow before shutdown", streak: 14, done: true },
      { name: "Run or walk", streak: 9, done: true },
      { name: "Phone away after 8pm", streak: 21, done: false },
      { name: "Read 20 minutes", streak: 6, done: false },
      { name: "Strength or mobility", streak: 4, done: false },
    ],
  },
  {
    key: "engineer", title: "Software engineer", icon: "code",
    blurb: "Technical impact and a clean shutdown",
    cycle: "Staff engineer impact cycle",
    vision: "By week 12 checkout reliability is measurably better, the migration plan is trusted, and my promotion packet has real evidence.",
    goals: [
      { title: "Ship the checkout reliability project, promotion-ready", pct: 40, ratio: "2/5", next: "Ship retry + idempotency fixes behind flags" },
      { title: "Complete 30 strength or mobility sessions", pct: 25, ratio: "1/4", next: "Lock a 35-minute strength template" },
      { title: "Protect 40 laptop-free evenings after 8pm", pct: 75, ratio: "3/4", next: "Keep the shutdown streak visible" },
    ],
    weekPriority: "Lead the reliability design review and unblock the migration",
    weekSupport: [
      { t: "Run the reliability design review", s: "doing" },
      { t: "Resolve open questions from the platform team", s: "open" },
      { t: "Draft promotion-evidence notes", s: "open" },
      { t: "Mobility fallback on release day", s: "open" },
    ],
    mission: "Prepare the design review notes",
    secondary: [
      { t: "Send the platform team open questions", done: false },
      { t: "Strength, upper body", done: true },
      { t: "Close the laptop before dinner", done: false },
    ],
    additional: [
      { t: "Reply to the recruiter", done: false },
      { t: "Pick up groceries", done: true },
    ],
    schedule: [
      { time: "7:00a", label: "Strength, upper body" },
      { time: "9:00a", label: "Deep work: design review", now: true },
      { time: "1:00p", label: "Platform questions" },
      { time: "6:30p", label: "Shutdown, then dinner" },
    ],
    note: "Make the design review boring in the best way: clear tradeoffs, a clear owner map, a clear rollback plan.",
    captures: [
      { time: "9:20a", text: "Open question: who owns mobile checkout retry telemetry?" },
      { time: "10:48a", text: "Ask my manager if the packet should include mentoring examples." },
    ],
    habits: [
      { name: "Write the shutdown note", streak: 12, done: true },
      { name: "Strength or walk", streak: 7, done: false },
      { name: "Laptop closed after 8pm", streak: 18, done: true },
      { name: "Read one design page", streak: 5, done: false },
      { name: "Review one pull request", streak: 9, done: false },
    ],
  },
  {
    key: "finance", title: "Finance professional", icon: "chart",
    blurb: "Forecast ownership and a steady study cadence",
    cycle: "Forecast leadership cycle",
    vision: "By week 12 the forecast package is board-ready, variance is under 3%, and study blocks are steady instead of last-minute.",
    goals: [
      { title: "Deliver a board-ready forecast, variance under 3%", pct: 40, ratio: "2/5", next: "Build the variance bridge for the top 5 drivers" },
      { title: "Complete 36 focused study blocks", pct: 25, ratio: "1/4", next: "Create a 120-question review set" },
      { title: "Protect 36 work-free evenings after 8pm", pct: 50, ratio: "2/4", next: "Write tomorrow's top variance question before shutdown" },
    ],
    weekPriority: "Turn owner inputs into a clean variance bridge",
    weekSupport: [
      { t: "Finish the top-5 driver bridge", s: "doing" },
      { t: "Reconcile sales-capacity assumptions", s: "open" },
      { t: "Run the Friday practice set", s: "open" },
      { t: "Prep the CFO review agenda", s: "open" },
    ],
    mission: "Reconcile the sales-capacity assumptions",
    secondary: [
      { t: "Send owner follow-ups before noon", done: false },
      { t: "Walk 30 minutes", done: true },
      { t: "Work phone away after dinner", done: false },
    ],
    additional: [
      { t: "Reply to the tax preparer", done: false },
      { t: "Pick up dry cleaning", done: true },
    ],
    schedule: [
      { time: "7:00a", label: "Morning walk" },
      { time: "9:00a", label: "Deep work: sales assumptions", now: true },
      { time: "1:00p", label: "Owner follow-ups" },
      { time: "7:45p", label: "Shutdown, phone away" },
    ],
    note: "Clean up the sales-capacity assumption before it becomes the whole CFO conversation.",
    captures: [
      { time: "8:30a", text: "Ask sales ops for current ramp by cohort." },
      { time: "12:10p", text: "Study block can slide to lunch if the CFO review runs long." },
    ],
    habits: [
      { name: "Write tomorrow's variance question", streak: 10, done: true },
      { name: "Walk or train", streak: 22, done: true },
      { name: "Work phone away after 8pm", streak: 15, done: false },
      { name: "Study 45 minutes", streak: 8, done: false },
      { name: "Update the forecast log", streak: 6, done: false },
    ],
  },
  {
    key: "operations", title: "Operations leader", icon: "team",
    blurb: "Cycle-time, manager rhythm, team execution",
    cycle: "Operations reset cycle",
    vision: "By week 12 onboarding is down from 21 days to 12, manager check-ins are consistent, and escalations show up early.",
    goals: [
      { title: "Cut onboarding cycle time from 21 to 12 days", pct: 40, ratio: "2/5", next: "Run a daily exception-review trial with team leads" },
      { title: "Walk 50 miles across the cycle", pct: 25, ratio: "1/4", next: "Add walking one-on-ones to two manager check-ins" },
      { title: "Protect 10 fully offline Saturday mornings", pct: 50, ratio: "2/4", next: "Move the work phone out of the bedroom Friday night" },
    ],
    weekPriority: "Run the daily exception-review trial with team leads",
    weekSupport: [
      { t: "Run 3 exception reviews", s: "doing" },
      { t: "Update the onboarding owner map", s: "open" },
      { t: "Schedule two walking one-on-ones", s: "open" },
      { t: "Write the customer escalation rules", s: "open" },
    ],
    mission: "Publish the onboarding owner map",
    secondary: [
      { t: "Send escalation rules to managers", done: false },
      { t: "Walk 2 miles", done: true },
      { t: "Choose the Saturday offline plan", done: false },
    ],
    additional: [
      { t: "Reply to the contractor", done: false },
      { t: "Confirm Friday dinner", done: true },
    ],
    schedule: [
      { time: "7:00a", label: "Morning walk" },
      { time: "9:00a", label: "Deep work: owner map", now: true },
      { time: "1:00p", label: "Manager review" },
      { time: "6:30p", label: "Shutdown, phone parked" },
    ],
    note: "Publish the owner map and make the new rhythm visible before the next customer handoff.",
    captures: [
      { time: "8:35a", text: "Use plain language for the escalation rules." },
      { time: "11:30a", text: "Ask team leads which metric belongs on the board first." },
    ],
    habits: [
      { name: "Set tomorrow's operating priority", streak: 16, done: true },
      { name: "Walk or lift", streak: 11, done: true },
      { name: "Work phone parked after dinner", streak: 19, done: false },
      { name: "Read one operations case", streak: 7, done: false },
      { name: "Update the metric board", streak: 5, done: false },
    ],
  },
];

// derived helpers
function personaStats(p) {
  const today = [{ done: false }, ...p.secondary, ...p.additional];
  const done = today.filter((x) => x.done).length;
  const habitsDone = p.habits.filter((h) => h.done).length;
  const best = Math.max(...p.habits.map((h) => h.streak));
  return { taskDone: done, taskTotal: today.length, habitsDone, habitsTotal: p.habits.length, best };
}

const PersonaContext = React.createContext(null);
function PersonaProvider({ children }) {
  const [key, setKey] = React.useState(PERSONAS[0].key);
  const persona = PERSONAS.find((p) => p.key === key) || PERSONAS[0];
  return React.createElement(PersonaContext.Provider, { value: { persona, key, setKey, list: PERSONAS } }, children);
}
function usePersona() { const c = React.useContext(PersonaContext); return (c && c.persona) || PERSONAS[0]; }
function usePersonaControls() { const c = React.useContext(PersonaContext) || {}; return [c.key || PERSONAS[0].key, c.setKey || (() => {}), c.list || PERSONAS]; }

// ── the switcher UI ──
function PersonaSwitcher({ label = "See it through someone real", center = false }) {
  const T = TOKENS;
  const [key, setKey, list] = usePersonaControls();
  return (
    <div style={{ width: "100%" }}>
      <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 12, justifyContent: center ? "center" : "flex-start" }}>
        <span style={{ fontFamily: FONTS.mono, fontSize: 11, fontWeight: 600, letterSpacing: 0.7, textTransform: "uppercase", color: T.textMuted }}>{label}</span>
      </div>
      <div className="persona-row" style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 10 }}>
        {list.map((p) => {
          const on = p.key === key;
          return (
            <button key={p.key} onClick={() => setKey(p.key)} style={{
              display: "flex", flexDirection: "column", gap: 8, padding: "14px 14px", textAlign: "left", cursor: "pointer",
              borderRadius: T.rMd, fontFamily: FONTS.sans,
              background: on ? T.accent : T.card,
              border: `1px solid ${on ? T.accent : T.hair}`,
              boxShadow: on ? `0 4px 14px -4px ${hexA(T.accent, 0.5)}` : T.shadowSm,
              transition: "all 160ms cubic-bezier(.32,.72,0,1)",
            }}>
              <span style={{ display: "inline-flex", width: 30, height: 30, borderRadius: 8, alignItems: "center", justifyContent: "center", background: on ? "rgba(255,255,255,0.18)" : T.accentSoft, flexShrink: 0 }}>
                <Icon name={p.icon} size={16} color={on ? "#fff" : T.accent} />
              </span>
              <span>
                <span style={{ display: "block", fontSize: 13.5, fontWeight: 600, letterSpacing: -0.2, color: on ? "#fff" : T.ink, lineHeight: 1.2 }}>{p.title}</span>
                <span style={{ display: "block", fontSize: 11.5, color: on ? "rgba(255,255,255,0.8)" : T.textMuted, lineHeight: 1.3, marginTop: 3 }}>{p.blurb}</span>
              </span>
            </button>
          );
        })}
      </div>
    </div>
  );
}

Object.assign(window, { PERSONAS, personaStats, PersonaContext, PersonaProvider, usePersona, usePersonaControls, PersonaSwitcher });
