-- Seed: five learning worlds + grade 6–8 sample objectives/exercises

INSERT INTO learning_worlds (code, name, description, sort_order) VALUES
  ('math_kingdom', 'Math Kingdom', 'Numbers, operations, and problem solving', 1),
  ('language_forest', 'Language Forest', 'Reading, writing, and communication', 2),
  ('world_discovery', 'World Discovery', 'Nature, science, and exploration', 3),
  ('creative_island', 'Creative Island', 'Art, music, and creative expression', 4),
  ('play_island', 'Play Island', 'Playful practice and collaborative games', 5)
ON CONFLICT (code) DO UPDATE SET
  name = EXCLUDED.name,
  description = EXCLUDED.description,
  sort_order = EXCLUDED.sort_order,
  is_active = TRUE,
  updated_at = NOW();

INSERT INTO subjects (world_id, code, name, sort_order)
SELECT w.id, v.code, v.name, v.sort_order
FROM (VALUES
  ('math_kingdom', 'math', 'Math', 1),
  ('language_forest', 'language', 'Language', 2),
  ('world_discovery', 'discovery', 'Discovery', 3),
  ('creative_island', 'creative', 'Creative', 4),
  ('play_island', 'play', 'Play', 5)
) AS v(world_code, code, name, sort_order)
INNER JOIN learning_worlds w ON w.code = v.world_code
ON CONFLICT (code) DO UPDATE SET
  name = EXCLUDED.name,
  world_id = EXCLUDED.world_id,
  sort_order = EXCLUDED.sort_order,
  is_active = TRUE,
  updated_at = NOW();

-- Objectives (per subject, grades 6–8)
WITH obj(subject_code, code, label, grade_min, grade_max, sort_order) AS (
  VALUES
    ('math', 'addition', 'Addition', 6, 8, 1),
    ('math', 'subtraction', 'Subtraction', 6, 8, 2),
    ('math', 'multiplication', 'Multiplication', 6, 8, 3),
    ('math', 'division', 'Division', 6, 8, 4),
    ('math', 'fractions', 'Fractions', 6, 8, 5),
    ('math', 'percentages', 'Percentages', 7, 8, 6),
    ('language', 'spelling', 'Spelling', 6, 8, 1),
    ('language', 'reading', 'Reading', 6, 8, 2),
    ('language', 'grammar', 'Grammar', 6, 8, 3),
    ('language', 'writing', 'Writing', 6, 8, 4),
    ('language', 'vocabulary', 'Vocabulary', 6, 8, 5),
    ('discovery', 'nature', 'Nature', 6, 8, 1),
    ('discovery', 'space', 'Space', 6, 8, 2),
    ('discovery', 'history', 'History', 6, 8, 3),
    ('discovery', 'geography', 'Geography', 6, 8, 4),
    ('discovery', 'science', 'Science', 6, 8, 5),
    ('creative', 'drawing', 'Drawing', 6, 8, 1),
    ('creative', 'music', 'Music', 6, 8, 2),
    ('creative', 'storytelling', 'Storytelling', 6, 8, 3),
    ('creative', 'design', 'Design', 6, 8, 4),
    ('play', 'puzzles', 'Puzzles', 6, 8, 1),
    ('play', 'team_challenges', 'Team Challenges', 6, 8, 2),
    ('play', 'quick_rounds', 'Quick Rounds', 6, 8, 3)
)
INSERT INTO learning_objectives (subject_id, code, label, grade_min, grade_max, sort_order)
SELECT s.id, obj.code, obj.label, obj.grade_min, obj.grade_max, obj.sort_order
FROM obj
INNER JOIN subjects s ON s.code = obj.subject_code
ON CONFLICT (subject_id, code) DO UPDATE SET
  label = EXCLUDED.label,
  grade_min = EXCLUDED.grade_min,
  grade_max = EXCLUDED.grade_max,
  sort_order = EXCLUDED.sort_order,
  is_active = TRUE,
  updated_at = NOW();

-- Two exercises per objective
INSERT INTO exercises (objective_id, code, title, difficulty, sort_order)
SELECT o.id, e.code, e.title, e.difficulty, e.sort_order
FROM learning_objectives o
CROSS JOIN LATERAL (
  VALUES
    ('basics', o.label || ' – Basics', 1, 1),
    ('practice', o.label || ' – Practice', 2, 2)
) AS e(code, title, difficulty, sort_order)
ON CONFLICT (objective_id, code) DO UPDATE SET
  title = EXCLUDED.title,
  difficulty = EXCLUDED.difficulty,
  sort_order = EXCLUDED.sort_order,
  is_active = TRUE,
  updated_at = NOW();
