// Извлекает CSS виджетов (.wg, .dnd-pool, .btn, .feedback, .sliders, .score-display, .spoiler)
// из physics_10_ch1.html в frontend/css/phys-textbook-widgets.css.
// Подключается из всех physics_8_*.html и physics_9_*.html файлов.
'use strict';
const fs = require('fs');
const path = require('path');

const SRC = path.join(__dirname, '..', '..', 'frontend', 'textbooks', 'physics_10_ch1.html');
const DST_CSS = path.join(__dirname, '..', '..', 'frontend', 'css', 'phys-textbook-widgets.css');

const h = fs.readFileSync(SRC, 'utf8');
const css = h.match(/<style>([\s\S]*?)<\/style>/)[1];

const start = css.indexOf('.btn{');
const end = css.indexOf('.col-side{');
if (start < 0 || end < 0) { console.error('Markers not found'); process.exit(1); }

const block = css.slice(start, end).trim();

const header =
  '/* Auto-extracted from frontend/textbooks/physics_10_ch1.html.\n' +
  ' * Provides .wg, .dnd-pool, .dnd-chip, .drop-box, .btn, .feedback,\n' +
  ' * .actions, .sliders, .score-display, .spoiler — shared interactive\n' +
  ' * widget styles for all physics-N chapter pages.\n' +
  ' * Generated by backend/scripts/extract_widget_css.cjs.\n' +
  ' */\n\n';

fs.writeFileSync(DST_CSS, header + block + '\n');
console.log('Wrote', DST_CSS, '(' + (header.length + block.length) + ' bytes)');

// === Inject <link> in all physics_8_*.html and physics_9_*.html that miss it ===
const TBOOKS = path.dirname(SRC);
const files = fs.readdirSync(TBOOKS).filter(f =>
  /^physics_[89]_(ch\d|hub|lab)\.html$/.test(f)
);

const LINK_TAG = '<link rel="stylesheet" href="/css/phys-textbook-widgets.css">';
const ANCHOR = '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">';

let injectedCount = 0;
for (const f of files) {
  const p = path.join(TBOOKS, f);
  let body = fs.readFileSync(p, 'utf8');
  if (body.includes('phys-textbook-widgets.css')) continue;
  if (!body.includes(ANCHOR)) {
    console.warn('  skip', f, '(no katex anchor)');
    continue;
  }
  body = body.replace(ANCHOR, ANCHOR + '\n' + LINK_TAG);
  fs.writeFileSync(p, body);
  injectedCount++;
  console.log('  injected →', f);
}
console.log('Total injected:', injectedCount, '/', files.length);
