-- Algebra 8 hub migration.
-- Adds parent_slug column so chapters can be grouped under a hub textbook.
-- The original algebra-8 row (id=3) becomes algebra-8-ch1; its id never changes
-- so all textbook_progress rows keep their FK references intact.

-- 1. Add nullable parent_slug column (idempotent: will fail gracefully if already exists,
--    but migrations-runner wraps each file in a transaction so this is fine as a fresh run).
ALTER TABLE textbooks ADD COLUMN parent_slug TEXT;

-- 2. Rename the existing chapter-1 slug from 'algebra-8' to 'algebra-8-ch1'.
--    Row id=3 is untouched, so textbook_progress.textbook_id=3 continues to resolve correctly.
UPDATE textbooks SET slug = 'algebra-8-ch1' WHERE slug = 'algebra-8';

-- 3. Insert the new hub row.
INSERT INTO textbooks
  (slug, subject, grade, title, author, description, html_path, para_count, color, sort_order, is_active)
VALUES
  ('algebra-8', 'math', 8, 'Алгебра — 8 класс', '',
   'Полный курс алгебры 8 класса: квадратные корни и действительные числа, квадратные уравнения, неравенства с одной переменной. 3 главы, 21 параграф + 3 финала, 100+ интерактивов, 21 босс-проверка.',
   'algebra_8_hub.html', 21, 'pink', 3, 1);

-- 4. Tag all three chapter rows as children of the hub.
UPDATE textbooks SET parent_slug = 'algebra-8' WHERE slug IN ('algebra-8-ch1', 'algebra-8-ch2', 'algebra-8-ch3');
