// atelier-data.jsx — Default lesson data: the Karpov-Spassky 1973 position.
// Sets window.LESSON to the full lesson schema, then adds backward-compat shims
// so existing components that read window.A_* continue to work unchanged.

const A_FILES = ['a','b','c','d','e','f','g','h'];

// Compute board-level influence by summing per-piece maps.
(function () {
  const position = {
    'a1':'wR','c1':'wB','d1':'wQ','e1':'wK','f1':'wB','h1':'wR',
    'a2':'wP','b2':'wP','c2':'wP','f2':'wP','g2':'wP','h2':'wP',
    'c3':'wN','d4':'wP','e4':'wP','f3':'wN',
    'a8':'bR','c8':'bB','d8':'bQ','e8':'bK','f8':'bB','h8':'bR',
    'a7':'bP','b7':'bP','c7':'bP','f7':'bP','g7':'bP','h7':'bP',
    'c6':'bN','d5':'bP','e5':'bP','f6':'bN',
  };

  const shapScores = {
    'f3':+0.87,'c3':+0.42,'d4':+0.35,'e4':+0.40,'d1':+0.55,'f1':+0.28,
    'c1':+0.22,'a1':+0.30,'h1':+0.18,'e1':+0.08,'f2':+0.06,'g2':+0.05,'h2':+0.04,
    'a2':+0.04,'b2':+0.04,'c2':+0.06,
    'f6':-0.78,'c6':-0.38,'d5':-0.20,'e5':-0.32,'d8':-0.48,'f8':-0.22,
    'c8':-0.18,'a8':-0.26,'h8':-0.15,'e8':-0.06,'f7':-0.05,'g7':-0.04,'h7':-0.03,
    'a7':-0.03,'b7':-0.04,'c7':-0.05,
  };

  const shapFactors = {
    'f3': [
      { label: 'Outpost potential (f5)',    value: +0.34 },
      { label: 'Central mobility',          value: +0.21 },
      { label: 'Coordinates with d1 queen', value: +0.18 },
      { label: 'King-side pressure',        value: +0.10 },
      { label: 'Cost: leaves f2 pawn lone', value: +0.04 },
    ],
    'f6': [
      { label: 'Defends king-side',         value: -0.28 },
      { label: 'Blocks e-file expansion',   value: -0.22 },
      { label: 'Attacks the e4 pawn',       value: -0.18 },
      { label: 'Cost: passive on f6',       value: -0.10 },
    ],
  };

  const trajectories = {
    'f3': ['f3','h4','f5'],
    'c3': ['c3','b5','d6'],
    'd1': ['d1','d3','f3'],
    'f1': ['f1','d3','f5'],
    'c1': ['c1','a3','b4'],
    'f6': ['f6','d7','b6'],
    'c6': ['c6','b4','d3'],
    'd8': ['d8','d7','g4'],
  };

  const influenceByPiece = {
    'f3': {'e5':0.85,'g5':0.75,'h4':0.65,'d4':0.4,'d2':0.4,'e1':0.3,'g1':0.3,'f5':0.5},
    'c3': {'b5':0.7,'d5':0.85,'e4':0.5,'a4':0.45,'e2':0.3,'b1':0.25,'d1':0.25,'a2':0.2},
    'd4': {'c5':0.7,'e5':0.7},
    'e4': {'d5':0.7,'f5':0.7},
    'd1': {'d2':0.6,'d3':0.55,'d4':0.5,'d5':0.45,'a1':0.3,'b1':0.3,'c1':0.3,'e1':0.3,'f1':0.3,'g1':0.3,'h1':0.3,'a4':0.35,'b3':0.45,'c2':0.4},
    'f1': {'e2':0.55,'d3':0.5,'c4':0.45,'b5':0.4,'a6':0.35,'g2':0.4},
    'c1': {'a3':0.4,'b2':0.55,'d2':0.55,'e3':0.5,'f4':0.45,'g5':0.4,'h6':0.35},
    'a1': {'a2':0.5,'a3':0.45,'a4':0.4,'a5':0.35,'a6':0.3,'a7':0.25,'a8':0.2,'b1':0.3},
    'h1': {'g1':0.4,'h2':0.5,'h3':0.4,'h4':0.35,'h5':0.3,'h6':0.25},
    'e1': {'d1':0.3,'d2':0.4,'e2':0.5,'f2':0.4,'f1':0.3},
    'f6': {'e4':-0.85,'g4':-0.75,'h5':-0.65,'d5':-0.4,'d7':-0.4,'e8':-0.3,'g8':-0.3,'f4':-0.5},
    'c6': {'b4':-0.7,'d4':-0.85,'e5':-0.5,'a5':-0.45,'e7':-0.3,'b8':-0.25,'d8':-0.25,'a7':-0.2},
    'd5': {'c4':-0.7,'e4':-0.7},
    'e5': {'d4':-0.7,'f4':-0.7},
    'd8': {'d7':-0.6,'d6':-0.55,'d5':-0.5,'a8':-0.3,'b8':-0.3,'c8':-0.3,'e8':-0.3,'f8':-0.3,'g8':-0.3,'h8':-0.3,'a5':-0.35,'b6':-0.45,'c7':-0.4},
    'f8': {'e7':-0.55,'d6':-0.5,'c5':-0.45,'b4':-0.4,'a3':-0.35,'g7':-0.4},
    'c8': {'a6':-0.4,'b7':-0.55,'d7':-0.55,'e6':-0.5,'f5':-0.45,'g4':-0.4,'h3':-0.35},
  };

  // Board-level summed influence for the heatmap (positive = white, negative = black).
  const influenceBoard = (() => {
    const out = {};
    for (const piece of Object.keys(influenceByPiece)) {
      const inf = influenceByPiece[piece];
      for (const [sq, v] of Object.entries(inf)) {
        out[sq] = (out[sq] || 0) + v;
      }
    }
    // Add a touch for the piece's own square so it isn't dark.
    for (const piece of Object.keys(influenceByPiece)) {
      const sign = shapScores[piece] >= 0 ? 1 : -1;
      out[piece] = (out[piece] || 0) + sign * 0.2;
    }
    // Clamp
    for (const k of Object.keys(out)) out[k] = Math.max(-1, Math.min(1, out[k] * 0.5));
    return out;
  })();

  const hero = 'f3';

  const tensions = [
    { defender: 'e5', defendingSide: 'b', load: 2, support: 1,
      attackers: [
        { from: 'd4', kind: 'pawn' },
        { from: 'f3', kind: 'knight' },
      ],
      supporters: [{ from: 'c6', kind: 'knight' }],
      note: 'e5 is held only by the c6 knight. The d4 pawn and f3 knight both attack it. The pawn cannot hold a trade.',
    },
    { defender: 'd5', defendingSide: 'b', load: 2, support: 2,
      attackers: [
        { from: 'e4', kind: 'pawn' },
        { from: 'c3', kind: 'knight' },
      ],
      supporters: [
        { from: 'f6', kind: 'knight' },
        { from: 'd8', kind: 'queen' },
      ],
      note: 'd5 holds. The f6 knight and d8 queen both back it up, but the queen is tied to this square.',
    },
  ];

  // Lesson sequence (Lesson 7 · "The Promise of the f5 Square").
  const lesson = [
    {
      title: 'Sit with the board',
      body: 'White to move. Before doing anything, just look. Notice that the knight on f3 has nowhere especially exciting to be — yet. Notice where Black’s weakness lives.',
      layers: { heatmap: false, hero: false, trajectory: false, tension: false },
      focus: null,
    },
    {
      title: 'Where does each side rule?',
      body: 'The heatmap shows where each colour holds influence. White owns the queenside diagonals and the e3–f3 wedge. Black owns the long c6–f6 stretch. The d/e files are contested.',
      layers: { heatmap: true, hero: false, trajectory: false, tension: false },
      focus: null,
    },
    {
      title: 'Meet the Hero Piece',
      body: 'Of every piece on the board, the f3 knight contributes the most to the evaluation. Not because of what it does now, but because of what it can become. SHAP value: +0.87.',
      layers: { heatmap: true, hero: true, trajectory: false, tension: false },
      focus: 'f3',
    },
    {
      title: 'Imagine the route',
      body: 'Nh4 looks passive — but it unlocks Nf5, an outpost where Black would need to push e6 or g6 to challenge it, both weakening the king.',
      layers: { heatmap: true, hero: true, trajectory: true, tension: false },
      focus: 'f3',
    },
    {
      title: 'Find the thread under strain',
      body: 'Look at e5. One guard — the c6 knight. Two attackers — the d4 pawn and your f3 knight. You could take it now. But that is not the plan. Seeing the overload is the first step. Choosing not to cash it in is the second.',
      layers: { heatmap: false, hero: false, trajectory: false, tension: true },
      focus: 'e5',
    },
    {
      title: 'Put it together',
      body: 'The plan is clear. Reroute the knight to f5. The c3 knight and e4 pawn keep d5 under pressure. The d8 queen is tied down defending it. The f5 knight creates threats Black cannot answer with pawns alone. No tactic required — just a good piece in a permanent home.',
      layers: { heatmap: true, hero: true, trajectory: true, tension: true },
      focus: 'f3',
    },
  ];

  const coaching = [
    { anchor: 'f3', mood: 'curious', text: 'This knight is doing more than it looks. What square can it aim for?' },
    { anchor: 'e5', mood: 'alert',   text: 'One defender, two attackers. Count before you calculate.' },
    { anchor: 'd5', mood: 'calm',    text: 'Balanced for now — but the queen is pinned to this duty.' },
  ];

  window.LESSON = {
    id: 'karpov-spassky-1973',
    schemaVersion: 1,
    meta: {
      title: 'The Promise of the f5 Square',
      subtitle: 'A knight finds its permanent home while Black’s defences hold by a thread.',
      theme: 'outpost',
      difficulty: 'intermediate',
      estimatedMinutes: 8,
      status: 'published',
      fen: 'r1bqkb1r/ppp2ppp/2n2n2/3pp3/3PP3/2N2N2/PPP2PPP/R1BQKB1R w KQkq - 0 1',
      sideToMove: 'white',
      lichessRating: 1650,
    },
    position,
    engine: { evaluation: 1.42, bestMove: 'Nh4' },
    shapScores,
    shapFactors,
    trajectories,
    influenceByPiece,
    influenceBoard,
    hero,
    tensions,
    lesson,
    coaching,
  };

  // Backward-compat shims: existing components read window.A_* and continue to work.
  window.A_POSITION           = window.LESSON.position;
  window.A_SHAP               = window.LESSON.shapScores;
  window.A_SHAP_FACTORS       = window.LESSON.shapFactors;
  window.A_TRAJECTORIES       = window.LESSON.trajectories;
  window.A_INFLUENCE_BY_PIECE = window.LESSON.influenceByPiece;
  window.A_INFLUENCE_BOARD    = window.LESSON.influenceBoard;
  window.A_HERO               = window.LESSON.hero;
  window.A_TENSIONS           = window.LESSON.tensions;
  window.A_LESSON             = window.LESSON.lesson;

  window.A_FILES = A_FILES;
})();
