// atelier-screens-h.jsx — DashboardScreen (Block C).
//
// Reads /api/dashboard when signed in; falls back to localStorage
// aggregates when not. Per the design-review revisions:
//   - Mobile (≤880px): phase bars → drill bars → SORTED LIST of
//     piece × motif rows. No grid.
//   - Desktop (>880px): piece × motif as a 6×5 heatmap.
//   - No "73% accuracy" — only "18 of 24 correct" framing.

const { useState: useSH, useEffect: useEH, useMemo: useMH } = React;

const PIECES_ORDER = ['knight', 'bishop', 'rook', 'queen', 'pawn', 'king'];
const MOTIFS_ORDER = ['pin', 'fork', 'discovered-attack', 'skewer', 'knight-endgame'];
const PIECE_LABEL = { knight: 'Knight', bishop: 'Bishop', rook: 'Rook', queen: 'Queen', pawn: 'Pawn', king: 'King' };
const MOTIF_LABEL = { pin: 'Pin', fork: 'Fork', 'discovered-attack': 'Discovered', skewer: 'Skewer', 'knight-endgame': 'Knight EG' };
const PHASE_LABEL = { look: 'Look', heatmap: 'Heatmap', hero: 'Hero', tension: 'Tension', solve: 'Solve' };

function accuracyTone(palette, attempts, correct) {
  const C = palette;
  if (!attempts) return { bg: 'transparent', fg: C.umberFaint };
  const ratio = correct / attempts;
  if (ratio >= 0.85) return { bg: window.withAlpha(C.celadon || '#8FA391', 0.22), fg: C.umber };
  if (ratio >= 0.6)  return { bg: window.withAlpha(C.celadon || '#8FA391', 0.10), fg: C.umber };
  if (ratio >= 0.4)  return { bg: window.withAlpha(C.sienna   || '#C97B5F', 0.10), fg: C.umberSoft };
  return { bg: window.withAlpha(C.sienna || '#C97B5F', 0.20), fg: C.umberSoft };
}

function DashboardScreen({ palette, setRoute }) {
  const C = palette;
  const isMobile = window.useBreakpoint().isMobile;
  const [data, setData] = useSH(null);
  const [loading, setLoading] = useSH(true);
  const [user, setUser] = useSH(window.CURRENT_USER || null);

  useEH(() => {
    const onUser = (e) => setUser((e && e.detail) || null);
    window.addEventListener('userChanged', onUser);
    return () => window.removeEventListener('userChanged', onUser);
  }, []);

  useEH(() => {
    let cancelled = false;
    async function load() {
      setLoading(true);
      if (user) {
        try {
          const res = await fetch('/api/dashboard', { credentials: 'include' });
          if (res.ok) {
            const json = await res.json();
            if (!cancelled) { setData(json); setLoading(false); return; }
          }
        } catch (_) {}
      }
      // Local fallback: synthesise from localStorage progress.
      const progress = (window.getProgress && window.getProgress()) || {};
      const lessonsCompleted = Object.values(progress).filter((p) => p && p.complete).length;
      if (!cancelled) {
        setData({
          totals: { attempts: 0, lessonsCompleted },
          pieceMotif: [],
          phase: [],
          drills: [],
        });
        setLoading(false);
      }
    }
    load();
    return () => { cancelled = true; };
  }, [user]);

  if (loading) {
    return <div style={{ flex: 1, padding: 48, color: C.umberSoft, fontFamily: window.ATELIER_TYPE.display, fontStyle: 'italic' }}>Loading…</div>;
  }

  const totals = data && data.totals ? data.totals : { attempts: 0, lessonsCompleted: 0 };
  const phaseRows = data && data.phase ? data.phase : [];
  const drillRows = data && data.drills ? data.drills : [];
  const pieceMotifRows = data && data.pieceMotif ? data.pieceMotif : [];

  // Build a {piece}|{motif} → row map for fast cell lookup.
  const pmMap = useMH(() => {
    const m = {};
    for (const r of pieceMotifRows) m[r.piece + '|' + r.motif] = r;
    return m;
  }, [pieceMotifRows]);

  const sortedPM = useMH(() => {
    return [...pieceMotifRows].sort((a, b) => b.attempts - a.attempts);
  }, [pieceMotifRows]);

  return (
    <div style={{ flex: 1, height: '100%', overflow: 'auto', background: C.bone }}>
      <section className="atelier-section-pad" style={{ padding: isMobile ? '40px 20px 12px' : '56px 48px 12px' }}>
        <div style={{
          fontFamily: window.ATELIER_TYPE.ui, fontSize: 10, letterSpacing: 3.5,
          textTransform: 'uppercase', opacity: 0.55, color: C.umberSoft, marginBottom: 8,
        }}>Where you've spent time</div>
        <h1 style={{
          margin: 0,
          fontFamily: window.ATELIER_TYPE.display, fontWeight: 300, fontStyle: 'italic',
          fontSize: isMobile ? 36 : 48, lineHeight: 1.05, color: C.umber, letterSpacing: -0.5,
        }}>Dashboard.</h1>
        <p style={{ marginTop: 14, fontFamily: window.ATELIER_TYPE.display, fontSize: 16, color: C.umberSoft, maxWidth: 600 }}>
          {totals.attempts} attempts · {totals.lessonsCompleted} lessons completed.
        </p>
        {!user && (
          <p style={{ marginTop: 12, fontFamily: window.ATELIER_TYPE.display, fontSize: 14, color: C.umberFaint, maxWidth: 600 }}>
            Sign in to see history from other devices.
          </p>
        )}
      </section>

      {/* Phase bars */}
      <section className="atelier-section-pad" style={{ padding: isMobile ? '24px 20px 12px' : '32px 48px 12px' }}>
        <window.SectionHeading palette={C} kicker="By phase" title="Where the misses land" small />
        <div style={{ marginTop: 16, display: 'flex', flexDirection: 'column', gap: 10 }}>
          {phaseRows.length === 0 && <EmptyNote C={C}>No phase data yet.</EmptyNote>}
          {phaseRows.map((r) => <PhaseBar key={r.phase} palette={C} row={r} />)}
        </div>
      </section>

      {/* Drill bars */}
      {drillRows.length > 0 && (
        <section className="atelier-section-pad" style={{ padding: isMobile ? '12px 20px 12px' : '12px 48px 12px' }}>
          <window.SectionHeading palette={C} kicker="Drills" title="Geometry warm-ups" small />
          <div style={{ marginTop: 16, display: 'flex', flexDirection: 'column', gap: 10 }}>
            {drillRows.map((r) => <DrillBar key={r.drill} palette={C} row={r} />)}
          </div>
        </section>
      )}

      {/* Piece × Motif: list on mobile, grid on desktop */}
      <section className="atelier-section-pad" style={{ padding: isMobile ? '24px 20px 48px' : '32px 48px 56px' }}>
        <window.SectionHeading palette={C} kicker="Piece × motif" title="The cells you've touched" small />
        {sortedPM.length === 0 ? (
          <EmptyNote C={C} style={{ marginTop: 16 }}>Haven't tried any positions yet.</EmptyNote>
        ) : isMobile ? (
          <div style={{ marginTop: 16, display: 'flex', flexDirection: 'column' }}>
            {sortedPM.map((r) => <PieceMotifRow key={r.piece + '|' + r.motif} palette={C} row={r} />)}
          </div>
        ) : (
          <PieceMotifGrid palette={C} pmMap={pmMap} />
        )}
      </section>
    </div>
  );
}

function EmptyNote({ C, children, style }) {
  return (
    <p style={{ margin: '12px 0 0', fontFamily: window.ATELIER_TYPE.display, fontStyle: 'italic',
                fontSize: 15, color: C.umberFaint, ...(style || {}) }}>
      {children}
    </p>
  );
}

function PhaseBar({ palette, row }) {
  const C = palette;
  const tone = accuracyTone(C, row.attempts, row.correct);
  const widthPct = Math.round((row.correct / Math.max(row.attempts, 1)) * 100);
  return (
    <div>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 12, marginBottom: 4 }}>
        <div style={{ fontFamily: window.ATELIER_TYPE.display, fontStyle: 'italic', fontSize: 15, color: C.umber }}>
          {PHASE_LABEL[row.phase] || row.phase}
        </div>
        <div style={{ fontFamily: window.ATELIER_TYPE.ui, fontSize: 11, letterSpacing: 1, color: C.umberFaint }}>
          {row.correct} of {row.attempts} correct
        </div>
      </div>
      <div style={{ height: 4, background: C.umberHair, borderRadius: 99, overflow: 'hidden' }}>
        <div style={{ width: widthPct + '%', height: '100%', background: tone.bg, transition: 'width 400ms ease' }} />
      </div>
    </div>
  );
}

function DrillBar({ palette, row }) {
  const C = palette;
  return (
    <div style={{
      display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 12,
      padding: '8px 0', borderBottom: '0.6px solid ' + C.umberHair,
    }}>
      <div style={{ fontFamily: window.ATELIER_TYPE.display, fontStyle: 'italic', fontSize: 15, color: C.umber }}>
        {row.drill}
      </div>
      <div style={{ fontFamily: window.ATELIER_TYPE.ui, fontSize: 11, letterSpacing: 1, color: C.umberFaint }}>
        {row.correct} of {row.attempts} correct
      </div>
    </div>
  );
}

function PieceMotifRow({ palette, row }) {
  const C = palette;
  const tone = accuracyTone(C, row.attempts, row.correct);
  return (
    <div style={{
      display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', gap: 12,
      padding: '10px 0', borderBottom: '0.6px solid ' + C.umberHair,
    }}>
      <div>
        <span style={{ fontFamily: window.ATELIER_TYPE.display, fontStyle: 'italic', fontSize: 16, color: C.umber }}>
          {PIECE_LABEL[row.piece] || row.piece}
        </span>
        <span style={{
          margin: '0 8px',
          fontFamily: window.ATELIER_TYPE.ui, fontSize: 10, color: C.umberFaint,
        }}>·</span>
        <span style={{ fontFamily: window.ATELIER_TYPE.display, fontStyle: 'italic', fontSize: 16, color: C.umberSoft }}>
          {MOTIF_LABEL[row.motif] || row.motif}
        </span>
      </div>
      <div style={{
        fontFamily: window.ATELIER_TYPE.ui, fontSize: 11, letterSpacing: 1,
        color: tone.fg,
        padding: '2px 8px', borderRadius: 99,
        background: tone.bg,
      }}>
        {row.correct} of {row.attempts}
      </div>
    </div>
  );
}

function PieceMotifGrid({ palette, pmMap }) {
  const C = palette;
  return (
    <div style={{
      marginTop: 16, display: 'grid',
      gridTemplateColumns: '120px repeat(' + MOTIFS_ORDER.length + ', 1fr)',
      gap: 6,
      fontFamily: window.ATELIER_TYPE.ui,
    }}>
      <div></div>
      {MOTIFS_ORDER.map((m) => (
        <div key={m} style={{
          fontSize: 10, letterSpacing: 2, textTransform: 'uppercase',
          color: C.umberFaint, textAlign: 'center', padding: '6px 4px',
        }}>{MOTIF_LABEL[m] || m}</div>
      ))}
      {PIECES_ORDER.map((p) => (
        <React.Fragment key={p}>
          <div style={{
            fontSize: 12, letterSpacing: 1,
            color: C.umberSoft,
            display: 'flex', alignItems: 'center',
            paddingRight: 8,
            fontFamily: window.ATELIER_TYPE.display, fontStyle: 'italic',
            fontSize: 14,
          }}>{PIECE_LABEL[p]}</div>
          {MOTIFS_ORDER.map((m) => {
            const row = pmMap[p + '|' + m];
            const tone = accuracyTone(C, row ? row.attempts : 0, row ? row.correct : 0);
            return (
              <div key={p + '-' + m} style={{
                background: row ? tone.bg : 'transparent',
                border: '0.6px solid ' + C.umberHair,
                borderRadius: 6,
                padding: '12px 8px',
                textAlign: 'center',
                fontFamily: window.ATELIER_TYPE.ui,
                fontSize: 11,
                color: row ? tone.fg : C.umberFaint,
                letterSpacing: 0.5,
                minHeight: 44,
                display: 'flex', flexDirection: 'column',
                alignItems: 'center', justifyContent: 'center',
                gap: 2,
              }}>
                {row ? (
                  <>
                    <span style={{ fontWeight: 500 }}>{row.correct}/{row.attempts}</span>
                  </>
                ) : (
                  <span style={{ opacity: 0.4 }}>—</span>
                )}
              </div>
            );
          })}
        </React.Fragment>
      ))}
    </div>
  );
}

Object.assign(window, { DashboardScreen });
