// Results page — magazine ranking, mobile-first, no serif/italic
const { useMemo: useResMemo } = React;

function Results({ answers, stores, questions, onRestart, useIllustration, layout }) {
  const ranked = useResMemo(() => {
    return stores
      .map(s => ({ store: s, ...window.scoreStore(s, answers) }))
      .filter(r => r.score > 0)
      .sort((a, b) => b.score - a.score)
      .slice(0, 5);
  }, [stores, answers]);

  if (ranked.length === 0) {
    return (
      <div className="wrap" style={{ padding: '80px 20px', textAlign: 'center' }}>
        <p className="display-md" style={{ fontSize: 22 }}>条件に合うお店が<br/>見つかりませんでした。</p>
        <button className="btn" onClick={onRestart} style={{ marginTop: 24 }}>もう一度はじめる</button>
      </div>
    );
  }

  return (
    <div className="wrap" style={{ paddingTop: 28, paddingBottom: 100 }}>
      <ResultHeader answers={answers} questions={questions} ranked={ranked} onRestart={onRestart} />
      {layout === 'ranking' && <RankingLayout ranked={ranked} useIllustration={useIllustration} />}
      {layout === 'editorial' && <EditorialLayout ranked={ranked} useIllustration={useIllustration} />}
      {layout === 'list' && <ListLayout ranked={ranked} useIllustration={useIllustration} />}
      <Footer onRestart={onRestart} />
    </div>
  );
}

function ResultHeader({ answers, questions, ranked, onRestart }) {
  const summary = questions
    .map(q => {
      const a = answers[q.id];
      if (!a) return null;
      const lbl = Array.isArray(a)
        ? a.map(v => q.options.find(o => o.value === v)?.label).filter(Boolean).join('・')
        : q.options.find(o => o.value === a)?.label;
      return lbl;
    })
    .filter(Boolean);

  return (
    <header style={{ marginBottom: 36 }}>
      <div className="mono" style={{ fontSize: 10, letterSpacing: '0.2em', opacity: 0.6, marginBottom: 14 }}>
        GIFT GUIDE No.{String(new Date().getDate()).padStart(2, '0')} — CREAM TARO
      </div>
      <h1 className="display" style={{
        fontSize: 'var(--fs-display)',
        margin: '0 0 20px',
      }}>
        あなたに<br/>
        <span className="uline">ぴったりの</span><br/>
        {ranked.length}軒。
      </h1>
      <button className="btn ghost" onClick={onRestart} style={{ padding: '8px 16px', fontSize: 12, minHeight: 36 }}>
        ↻ もう一度えらぶ
      </button>

      <div className="rule" style={{ margin: '24px 0 14px' }}/>
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: '6px 8px', marginBottom: 14 }}>
        {summary.map((s, i) => (
          <span key={i} className="mono" style={{
            fontSize: 10, letterSpacing: '0.06em',
            padding: '4px 9px',
            border: '1px solid var(--line)',
            borderRadius: 999,
            background: 'var(--paper)',
            fontWeight: 500,
          }}>
            {s}
          </span>
        ))}
      </div>
      <div className="rule-soft"/>
    </header>
  );
}

function RankingLayout({ ranked, useIllustration }) {
  return (
    <div style={{ display: 'flex', flexDirection: 'column' }}>
      <HeroCard item={ranked[0]} useIllustration={useIllustration} />
      {ranked.slice(1).map((r, i) => (
        <RankRow key={r.store.id} rank={i + 2} item={r} useIllustration={useIllustration} />
      ))}
    </div>
  );
}

function HeroCard({ item, useIllustration }) {
  const { store, score } = item;
  return (
    <article style={{ borderBottom: '1px solid var(--line)', paddingBottom: 32, marginBottom: 8 }}
      className="hero-card">
      <div className="hero-grid">
        <div style={{ position: 'relative' }}>
          <Placeholder
            label={store.placeholder}
            illustration={useIllustration}
            accent={store.accent}
            ratio="5/6"
            style={{ borderRadius: 0 }}
          />
          <div className="display" style={{
            position: 'absolute', top: 10, left: 10,
            background: 'var(--ink)', color: 'var(--cream)',
            fontSize: 14, padding: '5px 10px',
            letterSpacing: '0.06em',
            fontWeight: 900,
          }}>
            No.1 PICK
          </div>
        </div>
        <div className="hero-body" style={{ paddingTop: 16 }}>
          <div className="mono" style={{ fontSize: 10, letterSpacing: '0.14em', opacity: 0.6, marginBottom: 8 }}>
            EDITOR'S CHOICE
          </div>
          <h2 className="display" style={{
            fontSize: 'var(--fs-h1)',
            margin: '0 0 8px',
          }}>
            {store.name}
          </h2>
          <div className="mono" style={{ fontSize: 11, letterSpacing: '0.12em', opacity: 0.7, marginBottom: 16 }}>
            {store.category} — {store.area}
          </div>
          <p style={{ fontSize: 15, lineHeight: 1.85, color: 'var(--ink-2)', margin: '0 0 20px', textWrap: 'pretty' }}>
            {store.desc}
          </p>
          <div style={{ display: 'flex', gap: 20, alignItems: 'center', marginBottom: 20, flexWrap: 'wrap' }}>
            <Meta label="価格帯" value={store.price} />
            <Meta label="マッチ度" value={`${Math.round(Math.min(score / 8, 1) * 100)}%`} />
          </div>
          <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
            <button className="btn">店舗ページを見る →</button>
            <button className="btn ghost">保存</button>
          </div>
        </div>
      </div>
      <style>{`
        .hero-grid { display: flex; flex-direction: column; gap: 0; }
        @media (min-width: 768px) {
          .hero-card { padding: 40px 0; }
          .hero-grid { display: grid; grid-template-columns: 1.1fr 1fr; gap: 48px; align-items: center; }
          .hero-body { padding-top: 0 !important; }
        }
      `}</style>
    </article>
  );
}

function RankRow({ rank, item, useIllustration }) {
  const { store } = item;
  return (
    <article className="rank-row" style={{
      borderBottom: '1px solid var(--line)',
      padding: '20px 0',
      display: 'grid',
      gap: 14,
    }}>
      <div className="rank-row-inner">
        <span className="display rank-num" style={{
          fontSize: 64, color: store.accent, lineHeight: 0.9,
        }}>
          {String(rank).padStart(2, '0')}
        </span>
        <Placeholder
          label={store.placeholder}
          illustration={useIllustration}
          accent={store.accent}
          ratio="1/1"
          style={{ borderRadius: 0, width: '100%' }}
        />
        <div className="rank-text">
          <h3 className="display-md" style={{ fontSize: 22, fontWeight: 700, margin: '0 0 4px' }}>
            {store.name}
          </h3>
          <div className="mono" style={{ fontSize: 10, letterSpacing: '0.1em', opacity: 0.65, marginBottom: 8 }}>
            {store.category} — {store.area}
          </div>
          <p style={{ fontSize: 14, lineHeight: 1.7, color: 'var(--ink-2)', margin: 0, textWrap: 'pretty' }}>
            {store.desc}
          </p>
          <div style={{
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            marginTop: 10, gap: 12,
          }}>
            <div className="mono" style={{ fontSize: 11, opacity: 0.7 }}>{store.price}</div>
            <button className="btn ghost" style={{ padding: '6px 12px', fontSize: 11, minHeight: 32 }}>詳しく →</button>
          </div>
        </div>
      </div>
      <style>{`
        .rank-row-inner {
          display: grid;
          grid-template-columns: 60px 80px 1fr;
          gap: 14px;
          align-items: start;
        }
        @media (min-width: 768px) {
          .rank-row { padding: 28px 0; }
          .rank-row-inner {
            grid-template-columns: 80px 200px 1fr;
            gap: 28px; align-items: center;
          }
          .rank-row-inner .rank-num { font-size: 80px !important; }
          .rank-text h3 { font-size: 26px !important; }
          .rank-text p { font-size: 15px !important; }
        }
      `}</style>
    </article>
  );
}

function Meta({ label, value }) {
  return (
    <div>
      <div className="mono" style={{ fontSize: 10, opacity: 0.55, letterSpacing: '0.12em', marginBottom: 3 }}>{label}</div>
      <div className="display-md" style={{ fontSize: 16, fontWeight: 700 }}>{value}</div>
    </div>
  );
}

function EditorialLayout({ ranked, useIllustration }) {
  return (
    <div>
      <HeroCard item={ranked[0]} useIllustration={useIllustration} />
      <div className="ed-grid">
        {ranked.slice(1).map((r, i) => (
          <article key={r.store.id} style={{ display: 'flex', flexDirection: 'column' }}>
            <div style={{ position: 'relative', marginBottom: 12 }}>
              <Placeholder
                label={r.store.placeholder}
                illustration={useIllustration}
                accent={r.store.accent}
                ratio="3/4"
                style={{ borderRadius: 0 }}
              />
              <div className="display" style={{
                position: 'absolute', top: -8, right: -8,
                background: 'var(--cream)', color: r.store.accent,
                fontSize: 36,
                padding: '2px 10px',
                border: '1px solid var(--ink)',
                lineHeight: 1.1,
              }}>
                {i + 2}
              </div>
            </div>
            <div className="mono" style={{ fontSize: 10, letterSpacing: '0.14em', opacity: 0.6, marginBottom: 4 }}>
              {r.store.category}
            </div>
            <h3 className="display-md" style={{ fontSize: 20, fontWeight: 700, margin: '0 0 6px' }}>
              {r.store.name}
            </h3>
            <p style={{ fontSize: 13, lineHeight: 1.7, color: 'var(--ink-2)', margin: '0 0 8px' }}>
              {r.store.desc}
            </p>
            <div className="mono" style={{ fontSize: 10, opacity: 0.6 }}>
              {r.store.area} — {r.store.price}
            </div>
          </article>
        ))}
      </div>
      <style>{`
        .ed-grid {
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 24px;
          padding: 24px 0;
        }
        @media (min-width: 768px) {
          .ed-grid {
            grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
            gap: 36px; padding: 36px 0;
          }
        }
      `}</style>
    </div>
  );
}

function ListLayout({ ranked, useIllustration }) {
  return (
    <div>
      {ranked.map((r, i) => (
        <article key={r.store.id} style={{
          display: 'grid', gridTemplateColumns: '40px 1fr', gap: 14,
          padding: '18px 0',
          borderBottom: i === ranked.length - 1 ? 'none' : '1px solid var(--line-soft)',
          alignItems: 'start',
        }}>
          <span className="display" style={{
            fontSize: 28, color: r.store.accent, lineHeight: 1,
            paddingTop: 4,
          }}>
            {String(i + 1).padStart(2, '0')}
          </span>
          <div>
            <h3 className="display-md" style={{ fontSize: 18, fontWeight: 700, margin: '0 0 4px' }}>{r.store.name}</h3>
            <div className="mono" style={{ fontSize: 10, opacity: 0.6, marginBottom: 6, letterSpacing: '0.06em' }}>
              {r.store.category} — {r.store.area}
            </div>
            <p style={{ fontSize: 13, lineHeight: 1.65, margin: 0, color: 'var(--ink-2)' }}>{r.store.desc}</p>
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 8 }}>
              <span className="mono" style={{ fontSize: 11, opacity: 0.7 }}>{r.store.price}</span>
              <button className="btn ghost" style={{ padding: '4px 10px', fontSize: 11, minHeight: 28 }}>詳しく →</button>
            </div>
          </div>
        </article>
      ))}
    </div>
  );
}

function Footer({ onRestart }) {
  return (
    <footer style={{ marginTop: 60, paddingTop: 24, borderTop: '1px solid var(--line)' }}>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
        <div className="hand" style={{ fontSize: 22, lineHeight: 1.5 }}>
          また贈り物に迷ったときは。
        </div>
        <div className="mono" style={{ fontSize: 10, opacity: 0.6, letterSpacing: '0.16em' }}>
          CREAM TARO — @cream_taro
        </div>
        <button className="btn" onClick={onRestart} style={{ width: '100%', maxWidth: 320 }}>
          もう一度はじめる ↻
        </button>
      </div>
    </footer>
  );
}

Object.assign(window, { Results });
