// fc-reveal-recommended.jsx — THE RECOMMENDATION.
// Strategy: the reveal earns belief (the aha); the paywall makes the offer. Our
// wedge is that the personalization is REAL where Noom/BetterMe fake it — so the
// reveal reads like a coach who's already studied THIS athlete's cut. It leads with
// the safe verdict, then a coaching write-up about their specific cut (engine `tip`),
// then the recommended cut strategy (what to use / cap / skip for their weigh-in),
// then the levers that still sharpen the plan in setup. The full week-by-week plan
// stays locked — that's what the paywall sells. Same engine numbers (usePosterData).

const rr = window;
const REC_CLAY = window.POSTER_CLAY || '#e9ddcd';

function RevealRecommended() {
  const data = rr.usePosterData();
  const { c, d, strats, stratIntro } = data;
  if (!c.tip) return null;
  const Num = rr.Num, PaceMeter = rr.RevealLabPaceMeter, CoachTip = rr.CoachTip;
  const tip = c.tip;

  const divisionLabel = d.division || d.sport;
  const dateStr = c.FC.fightDateShort || 'Jan 1';

  // shared strategy grouping + tick (from fc-reveal-recommended-options.jsx) so this
  // screen and the direction options read Using / Cap / Skip identically.
  const groups = rr.rroStratGroups ? rr.rroStratGroups(strats) : { using: strats, cap: [], skip: [] };
  const Tick = rr.RROTick;
  const GREEN = rr.RRO_GREEN || 'var(--green, #2f5a24)';
  const AMBER = rr.RRO_AMBER || 'var(--mustard, #a87b22)';

  // the levers that still sharpen the plan in setup (drop the strategy lever — shown above)
  const levers = (rr.buildLevers ? rr.buildLevers(data) : []).filter((l) => l.key !== 'strategy');

  const SectionTitle = ({ children }) => (
    <div style={{ fontFamily: 'var(--sans)', fontSize: 17, fontWeight: 700, letterSpacing: '-0.01em', color: 'var(--ink)' }}>{children}</div>
  );

  return (
    <rr.RevealFrame label="Reveal · ★ Recommended" footer={<rr.PrimaryNext label="See my full plan" sub="Your camp is built — yours to keep." />}>
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column' }}>

        {/* ══ ONE POSTER — intelligence chip + goal echo + make-weight number, together. The single anchor. ══ */}
        <div style={{ background: REC_CLAY, color: 'var(--ink)', padding: '24px 24px 26px' }}>
          {/* beat 1 · intelligence chip — names the real work the engine did (labour illusion, honestly true) */}
          <div style={{ display: 'inline-flex', alignItems: 'center', gap: 7, background: 'rgba(163,42,24,0.10)', borderRadius: 100, padding: '5px 12px 5px 9px', marginBottom: 18 }}>
            <svg width="12" height="12" viewBox="0 0 14 14" fill="none" style={{ flexShrink: 0 }}><circle cx="7" cy="7" r="6" stroke="var(--accent)" strokeWidth="1.3" /><path d="M4.3 7.1 L6.1 8.9 L9.7 4.9" stroke="var(--accent)" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" /></svg>
            <span style={{ fontFamily: 'var(--mono)', fontSize: 9, fontWeight: 700, letterSpacing: '0.07em', textTransform: 'uppercase', color: 'var(--accent)' }}>Built from your answers</span>
          </div>
          <div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: 14 }}>
            <div style={{ flex: 1 }}>
              <div style={{ fontFamily: 'var(--mono)', fontSize: 9.5, letterSpacing: '0.14em', textTransform: 'uppercase', color: 'var(--ink-3)', fontWeight: 700 }}>
                {d.sport} · {c.weighShort} weigh-in · {d.weeks} weeks out
              </div>
              <div style={{ fontFamily: 'var(--sans)', fontSize: 22, fontWeight: 700, letterSpacing: '-0.02em', lineHeight: 1.18, color: 'var(--ink)', textWrap: 'balance', marginTop: 12 }}>
                {d.firstName ? `${d.firstName} — your ` : 'Your '}safe line to make {divisionLabel} by <span style={{ color: 'var(--accent)' }}>{dateStr}</span>.
              </div>
            </div>
            {c.glyph && rr.SportGlyph && <rr.SportGlyph name={c.glyph} size={32} ink="var(--ink)" accent="var(--accent)" />}
          </div>

          <div style={{ height: 1, background: 'rgba(21,20,15,0.14)', margin: '22px 0 18px' }} />

          <div style={{ display: 'flex', alignItems: 'flex-end', justifyContent: 'space-between', gap: 12 }}>
            <div>
              <div style={{ fontFamily: 'var(--mono)', fontSize: 9.5, letterSpacing: '0.16em', textTransform: 'uppercase', color: 'var(--ink-3)', fontWeight: 700 }}>Make-weight target</div>
              <div style={{ display: 'flex', alignItems: 'flex-end', gap: 10, marginTop: 6 }}>
                {Num ? <Num size={78} weight={300} color="var(--ink)">{d.targetW.toFixed(1)}</Num> : <span style={{ fontFamily: 'var(--num)', fontSize: 78 }}>{d.targetW.toFixed(1)}</span>}
                <span style={{ fontFamily: 'var(--mono)', fontSize: 13, color: 'var(--ink-3)', fontWeight: 600, paddingBottom: 12 }}>KG</span>
              </div>
            </div>
            <div style={{ textAlign: 'right', paddingBottom: 6 }}>
              <div style={{ display: 'inline-flex', alignItems: 'center', gap: 6, fontFamily: 'var(--mono)', fontSize: 10, letterSpacing: '0.1em', textTransform: 'uppercase', fontWeight: 700, color: GREEN }}>
                <span style={{ width: 6, height: 6, borderRadius: '50%', background: GREEN }} />Safe pace
              </div>
              <div style={{ fontFamily: 'var(--num)', fontWeight: 500, fontSize: 22, letterSpacing: '-0.02em', color: 'var(--accent)', marginTop: 8 }}>−{d.cut.toFixed(1)}<span style={{ fontFamily: 'var(--mono)', fontSize: 11, fontWeight: 600, marginLeft: 3 }}>kg</span></div>
              <div style={{ fontFamily: 'var(--mono)', fontSize: 9, letterSpacing: '0.06em', textTransform: 'uppercase', color: 'var(--ink-3)', fontWeight: 600, marginTop: 3 }}>from today</div>
            </div>
          </div>

          {/* beat 3 · the descent line — the verdict made visual, landing on make-weight */}
          {rr.PlanGlide && (
            <div style={{ marginTop: 18 }}>
              <rr.PlanGlide startW={d.startW} targetW={d.targetW} campWeeks={d.weeks} todayWeek={0} water={d.water} width={345} />
            </div>
          )}

          {/* safe-pace meter (shared) — carried on the poster, not a second card */}
          {PaceMeter && <div style={{ marginTop: 6 }}><PaceMeter ratePct={d.ratePct} compact /></div>}
        </div>

        <div style={{ padding: '26px 24px 28px', display: 'flex', flexDirection: 'column' }}>

          {/* ══ COACH'S READ — styled like the onboarding coach cards (clay card,
               eyebrow + meta header, hairline, headline + read) ══ */}
          <div style={{ background: '#f1ddd5', borderRadius: 'var(--radius)', padding: '20px 20px 22px' }}>
            <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', gap: 10 }}>
              <span style={{ fontFamily: 'var(--display)', fontSize: 10.5, fontWeight: 600, letterSpacing: '0.16em', textTransform: 'uppercase', color: 'var(--ink-3)' }}>Coach&rsquo;s read</span>
              <span style={{ fontFamily: 'var(--mono)', fontSize: 9.5, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--rule-2)' }}>{d.sport} · {d.water ? 'safe pace' : 'real weight'}</span>
            </div>
            <div style={{ height: 1, background: 'rgba(21,20,15,0.12)', margin: '16px 0 14px' }} />
            <div style={{ fontFamily: 'var(--sans)', fontSize: 18, fontWeight: 600, lineHeight: 1.3, letterSpacing: '-0.01em', color: 'var(--ink)', textWrap: 'balance' }}>{tip.headline}</div>
            <div style={{ fontFamily: 'var(--sans)', fontSize: 13.5, lineHeight: 1.55, color: 'var(--ink-2)', marginTop: 10, textWrap: 'pretty' }}>{tip.body}</div>
          </div>

          {/* ══ RECOMMENDED CUT STRATEGY — calm Using / Cap / Skip groups (shared) ══ */}
          <div style={{ marginTop: 26, borderTop: '1px solid var(--rule)', paddingTop: 22 }}>
            <SectionTitle>Recommended cut strategy</SectionTitle>
            <div style={{ fontFamily: 'var(--sans)', fontSize: 13.5, lineHeight: 1.5, color: 'var(--ink-2)', marginTop: 9, textWrap: 'pretty' }}>{stratIntro}</div>

            <div style={{ marginTop: 18, display: 'flex', flexDirection: 'column', gap: 12 }}>
              <div style={{ fontFamily: 'var(--mono)', fontSize: 9, letterSpacing: '0.14em', textTransform: 'uppercase', fontWeight: 700, color: GREEN }}>Using</div>
              {groups.using.concat(groups.cap).map((s, i) => (
                <div key={i} style={{ display: 'flex', gap: 11, alignItems: 'flex-start' }}>
                  {Tick ? <Tick /> : <span style={{ flexShrink: 0, marginTop: 3, width: 16, height: 16, borderRadius: '50%', background: GREEN }} />}
                  <div style={{ flex: 1, minWidth: 0 }}>
                    <span style={{ fontFamily: 'var(--sans)', fontSize: 14.5, fontWeight: 600, color: 'var(--ink)' }}>{s.t}</span>
                    <span style={{ fontFamily: 'var(--sans)', fontSize: 13.5, color: 'var(--ink-3)', lineHeight: 1.45 }}> — {s.s}</span>
                  </div>
                </div>
              ))}
            </div>

            {groups.skip.length > 0 && (
              <div style={{ marginTop: 16, display: 'flex', gap: 10, alignItems: 'baseline' }}>
                <span style={{ flexShrink: 0, fontFamily: 'var(--mono)', fontSize: 9, letterSpacing: '0.14em', textTransform: 'uppercase', fontWeight: 700, color: 'var(--ink-3)' }}>Skipping</span>
                <div style={{ flex: 1, fontFamily: 'var(--sans)', fontSize: 13.5, color: 'var(--ink-2)', lineHeight: 1.5, textWrap: 'pretty' }}>
                  {groups.skip.map((s) => s.t).join(' and ')} — hard to time and not worth the muscle. We leave them off.
                </div>
              </div>
            )}

            {/* recommendations now; the athlete chooses the exact mix when they set up camp */}
            <div style={{ marginTop: 18, display: 'flex', gap: 9, alignItems: 'flex-start', background: 'var(--paper-2)', borderRadius: 12, padding: '12px 14px' }}>
              <svg width="14" height="14" viewBox="0 0 14 14" fill="none" style={{ flexShrink: 0, marginTop: 1 }}><circle cx="7" cy="7" r="6" stroke="var(--ink-3)" strokeWidth="1.2" /><path d="M7 6.2 V10 M7 4.1 v0.05" stroke="var(--ink-3)" strokeWidth="1.4" strokeLinecap="round" /></svg>
              <div style={{ flex: 1, fontFamily: 'var(--sans)', fontSize: 12.5, lineHeight: 1.5, color: 'var(--ink-3)', textWrap: 'pretty' }}>
                This is what we&rsquo;d recommend for your weigh-in. You choose the exact mix — and fine-tune it — when you set up camp.
              </div>
            </div>
          </div>

          {/* ══ BUILT FOR YOUR SPORT — bespoke: what THIS cut protects for their sport,
               with copy that flips when the pace is too hot to protect it. Replaces the
               category stat (already shown at sport-select) with sport-specific proof. ══ */}
          {(() => {
            const pr = (rr.FC_SPORT_PROTECT || {})[d.sport] || (rr.FC_SPORT_PROTECT || {}).Other || { tag: 'Strength · gas', protect: 'your strength and stamina across the whole fight.' };
            const ceiling = rr.SAFE_RATE_PCT || 1.0;
            const safe = d.ratePct <= ceiling + 0.005;
            const tagPhrase = pr.tag.toLowerCase().replace(/\s*·\s*/, ' and ').replace(/^your\s+/, '');
            const tone = safe ? GREEN : AMBER;
            const pulls = [
              { k: 'Protects', v: pr.tag },
              { k: 'Weigh-in', v: d.water ? 'Water window' : 'Real weight only' },
              { k: 'Cut pace', v: `${d.ratePct.toFixed(2)}%/wk` },
            ];
            return (
              <div style={{ marginTop: 26, borderTop: '1px solid var(--rule)', paddingTop: 22 }}>
                <div style={{ background: '#f1ddd5', borderRadius: 'var(--radius)', padding: '20px 20px 22px' }}>
                  <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', gap: 10 }}>
                    <span style={{ fontFamily: 'var(--display)', fontSize: 10.5, fontWeight: 600, letterSpacing: '0.16em', textTransform: 'uppercase', color: 'var(--ink-3)' }}>Built for your fight</span>
                    <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, fontFamily: 'var(--mono)', fontSize: 9, letterSpacing: '0.08em', textTransform: 'uppercase', fontWeight: 700, color: tone }}>
                      <span style={{ width: 5, height: 5, borderRadius: '50%', background: tone }} />{safe ? 'Protected' : 'Runs hot'}
                    </span>
                  </div>
                  <div style={{ height: 1, background: 'rgba(21,20,15,0.12)', margin: '16px 0 14px' }} />

                  <div style={{ fontFamily: 'var(--sans)', fontSize: 18, fontWeight: 600, lineHeight: 1.3, letterSpacing: '-0.01em', color: 'var(--ink)', textWrap: 'balance' }}>
                    {safe
                      ? `This cut protects your ${tagPhrase}.`
                      : `This pace could cost your ${tagPhrase}.`}
                  </div>
                  <div style={{ fontFamily: 'var(--sans)', fontSize: 13.5, lineHeight: 1.55, color: 'var(--ink-2)', marginTop: 10, textWrap: 'pretty' }}>
                    {pr.protect}{' '}
                    {safe
                      ? <React.Fragment>At <strong style={{ fontWeight: 600, color: 'var(--ink)' }}>{d.ratePct.toFixed(2)}%/wk</strong> you’re inside the safe line, so the weight comes off as fat, not muscle — that stays intact.</React.Fragment>
                      : <React.Fragment>At <strong style={{ fontWeight: 600, color: 'var(--ink)' }}>{d.ratePct.toFixed(2)}%/wk</strong> you’re past the 1%/wk line — the point where that starts to erode. Give the cut more time and it holds.</React.Fragment>}
                  </div>

                  {/* bespoke pull-outs */}
                  <div style={{ display: 'flex', marginTop: 18, paddingTop: 16, borderTop: '1px solid rgba(21,20,15,0.12)' }}>
                    {pulls.map((p, i) => (
                      <div key={p.k} style={{ flex: 1, paddingLeft: i ? 14 : 0, borderLeft: i ? '1px solid rgba(21,20,15,0.12)' : 'none' }}>
                        <div style={{ fontFamily: 'var(--mono)', fontSize: 8, letterSpacing: '0.08em', textTransform: 'uppercase', fontWeight: 700, color: 'var(--ink-3)' }}>{p.k}</div>
                        <div style={{ fontFamily: 'var(--sans)', fontSize: 13, fontWeight: 600, color: i === 2 ? tone : 'var(--ink)', marginTop: 6, lineHeight: 1.2 }}>{p.v}</div>
                      </div>
                    ))}
                  </div>

                  {/* new evidence (not the sport-select stat): the safety consensus this plan obeys */}
                  {rr.SourceTag && (
                    <div style={{ marginTop: 16 }}>
                      <div style={{ fontFamily: 'var(--sans)', fontSize: 12, lineHeight: 1.5, color: 'var(--ink-3)', textWrap: 'pretty' }}>
                        Sports-science consensus: keep a cut under ~5% of bodyweight and it won’t cost performance — every OnWeight plan is built to that ceiling.
                      </div>
                      <rr.SourceTag src="MDPI Healthcare 2022" rule="rgba(21,20,15,0.12)" top={12} />
                    </div>
                  )}
                </div>
              </div>
            );
          })()}

          {/* ══ WHAT STILL SHARPENS IT ══ */}
          {levers.length > 0 && (
            <div style={{ marginTop: 26, borderTop: '1px solid var(--rule)', paddingTop: 22 }}>
              <SectionTitle>Sharpens once you set up camp</SectionTitle>
              <div style={{ fontFamily: 'var(--mono)', fontSize: 9.5, letterSpacing: '0.06em', textTransform: 'uppercase', color: 'var(--accent)', fontWeight: 700, marginTop: 7 }}>{levers.length} {levers.length === 1 ? 'input' : 'inputs'} still sharpen your plan</div>
              <div style={{ marginTop: 15, display: 'flex', flexDirection: 'column', gap: 12 }}>
                {levers.map((lv) => (
                  <div key={lv.key} style={{ display: 'flex', gap: 12, alignItems: 'flex-start' }}>
                    <span style={{ flexShrink: 0, width: 17, height: 17, borderRadius: '50%', border: '1.5px solid var(--rule-2)', marginTop: 1 }} />
                    <div style={{ flex: 1, minWidth: 0 }}>
                      <div style={{ fontFamily: 'var(--sans)', fontSize: 14, fontWeight: 600, color: 'var(--ink)' }}>{lv.title}</div>
                      <div style={{ fontFamily: 'var(--sans)', fontSize: 12.5, color: 'var(--ink-3)', marginTop: 2, lineHeight: 1.4 }}>{lv.summary}</div>
                    </div>
                  </div>
                ))}
              </div>
            </div>
          )}

          {/* ══ LOCKED — the week-by-week plan is what the paywall sells. The strong close. ══ */}
          <div style={{ marginTop: 26, background: 'var(--paper)', border: '1px solid var(--rule)', borderRadius: 'var(--radius)', padding: '16px 18px', display: 'flex', alignItems: 'center', gap: 13 }}>
            <svg width="17" height="18" viewBox="0 0 12 13" fill="none" style={{ flexShrink: 0 }}><rect x="1.5" y="5.5" width="9" height="6.5" rx="1.5" stroke="var(--accent)" strokeWidth="1.2" /><path d="M3.5 5.5 V4 a2.5 2.5 0 0 1 5 0 V5.5" stroke="var(--accent)" strokeWidth="1.2" /></svg>
            <div style={{ flex: 1 }}>
              <div style={{ fontFamily: 'var(--sans)', fontSize: 14.5, color: 'var(--ink)', fontWeight: 600 }}>Your day-by-day plan is ready</div>
              <div style={{ fontFamily: 'var(--sans)', fontSize: 12.5, color: 'var(--ink-3)', marginTop: 1 }}>All {d.weeks * 7} days, mapped to fight day.</div>
            </div>
          </div>

        </div>
      </div>
    </rr.RevealFrame>
  );
}

Object.assign(window, { RevealRecommended });
