-- ═══════════════════════════════════════════════════════════════
-- 034: Per-achievement feature dependency
--
-- When admin (or a class teacher) turns a module off, the matching
-- achievements should disappear from the user's "Достижения" tab —
-- but ONLY when the user hasn't unlocked them yet. Already-earned
-- achievements stay visible (never take a reward away).
--
-- This migration adds `required_feature` to achievements. NULL = no
-- gating (always visible). The value matches the slug used by
-- /api/features (e.g. 'exam9', 'red_book', 'pet').
--
-- The actual filter lives in gamification/api.getAchievements: it
-- reads the user's merged features (global + class + free_student)
-- and drops every locked row whose required_feature is disabled.
-- ═══════════════════════════════════════════════════════════════

ALTER TABLE achievements ADD COLUMN required_feature TEXT;

-- ── Backfill ──────────────────────────────────────────────────
-- Group by module so future inserts inherit the pattern through
-- seedAchievements (which mirrors these defaults).

-- exam-prep (math9 + future exam tracks)
UPDATE achievements SET required_feature = 'exam9'
 WHERE group_slug = 'exam';

-- red book (collection of species)
UPDATE achievements SET required_feature = 'red_book'
 WHERE track LIKE 'red_book%';

-- biochem
UPDATE achievements SET required_feature = 'biochem'
 WHERE track = 'biochem';

-- lab (no feature_lab_enabled flag exists today — set anyway so the
-- linkage is correct if/when one is introduced)
UPDATE achievements SET required_feature = 'lab'
 WHERE track IN ('lab', 'lab_reactions');

-- classroom (joining lessons + teacher class size)
UPDATE achievements SET required_feature = 'classroom'
 WHERE track IN ('classroom', 'teacher');

-- live quiz
UPDATE achievements SET required_feature = 'live_quiz'
 WHERE track = 'live_quiz';

-- flashcards
UPDATE achievements SET required_feature = 'flashcards'
 WHERE track = 'flashcards';

-- pet
UPDATE achievements SET required_feature = 'pet'
 WHERE track = 'pet';

-- textbooks (paragraph reads, chapter completion)
UPDATE achievements SET required_feature = 'textbooks'
 WHERE track = 'tb_progress';

-- Minigames (hangman/crossword) — either of two features could grant
-- progress, so 'hangman' alone is too narrow. Leave NULL to keep them
-- visible even if one game is disabled; the other can still earn them.
-- (Explicit: no update needed.)
