DROP TABLE IF EXISTS student_requirement_snapshots_old;
CREATE TEMP TABLE student_requirement_snapshots_old AS
SELECT *
FROM student_requirement_snapshots
WHERE student_id IN (
    SELECT s.id
    FROM students s
    JOIN license_class_templates t ON t.id = s.template_id
    WHERE t.code IN ('C+CE', 'C1+C1E')
);

DELETE FROM license_template_requirements
WHERE template_id IN (
    SELECT id FROM license_class_templates WHERE code IN ('C+CE', 'C1+C1E')
);

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C::practice', 'Uebungsstunden', 8, 0, 'C'
FROM license_class_templates
WHERE code = 'C+CE';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C::rural', 'Ueberland', 5, 1, 'C'
FROM license_class_templates
WHERE code = 'C+CE';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C::highway', 'Autobahn', 4, 2, 'C'
FROM license_class_templates
WHERE code = 'C+CE';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C::night', 'Nachtfahrt', 3, 3, 'C'
FROM license_class_templates
WHERE code = 'C+CE';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'CE::practice', 'Uebungsstunden', 6, 100, 'CE'
FROM license_class_templates
WHERE code = 'C+CE';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'CE::rural', 'Ueberland', 3, 101, 'CE'
FROM license_class_templates
WHERE code = 'C+CE';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'CE::highway', 'Autobahn', 3, 102, 'CE'
FROM license_class_templates
WHERE code = 'C+CE';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'CE::night', 'Nachtfahrt', 2, 103, 'CE'
FROM license_class_templates
WHERE code = 'C+CE';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C1::practice', 'Uebungsstunden', 6, 0, 'C1'
FROM license_class_templates
WHERE code = 'C1+C1E';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C1::rural', 'Ueberland', 4, 1, 'C1'
FROM license_class_templates
WHERE code = 'C1+C1E';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C1::highway', 'Autobahn', 3, 2, 'C1'
FROM license_class_templates
WHERE code = 'C1+C1E';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C1::night', 'Nachtfahrt', 2, 3, 'C1'
FROM license_class_templates
WHERE code = 'C1+C1E';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C1E::practice', 'Uebungsstunden', 4, 100, 'C1E'
FROM license_class_templates
WHERE code = 'C1+C1E';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C1E::rural', 'Ueberland', 2, 101, 'C1E'
FROM license_class_templates
WHERE code = 'C1+C1E';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C1E::highway', 'Autobahn', 2, 102, 'C1E'
FROM license_class_templates
WHERE code = 'C1+C1E';

INSERT INTO license_template_requirements (template_id, requirement_key, label, required_units, sort_order, phase_label)
SELECT id, 'C1E::night', 'Nachtfahrt', 1, 103, 'C1E'
FROM license_class_templates
WHERE code = 'C1+C1E';

UPDATE appointment_units
SET requirement_key = 'C::' || requirement_key
WHERE requirement_key IN ('practice', 'rural', 'highway', 'night')
  AND appointment_id IN (
      SELECT a.id
      FROM appointments a
      JOIN students s ON s.id = a.student_id
      JOIN license_class_templates t ON t.id = s.template_id
      WHERE t.code = 'C+CE'
  );

UPDATE appointment_units
SET requirement_key = 'C1::' || requirement_key
WHERE requirement_key IN ('practice', 'rural', 'highway', 'night')
  AND appointment_id IN (
      SELECT a.id
      FROM appointments a
      JOIN students s ON s.id = a.student_id
      JOIN license_class_templates t ON t.id = s.template_id
      WHERE t.code = 'C1+C1E'
  );

DELETE FROM student_requirement_snapshots
WHERE student_id IN (
    SELECT s.id
    FROM students s
    JOIN license_class_templates t ON t.id = s.template_id
    WHERE t.code IN ('C+CE', 'C1+C1E')
);

INSERT INTO student_requirement_snapshots (student_id, requirement_key, label, required_units, sort_order, prior_completed_units, phase_label)
SELECT
    s.id,
    r.requirement_key,
    r.label,
    r.required_units,
    r.sort_order,
    CASE
        WHEN r.requirement_key = 'C::practice' THEN COALESCE((
            SELECT prior_completed_units
            FROM student_requirement_snapshots_old old
            WHERE old.student_id = s.id AND old.requirement_key = 'practice'
        ), 0)
        WHEN r.requirement_key = 'C::rural' THEN COALESCE((
            SELECT prior_completed_units
            FROM student_requirement_snapshots_old old
            WHERE old.student_id = s.id AND old.requirement_key = 'rural'
        ), 0)
        WHEN r.requirement_key = 'C::highway' THEN COALESCE((
            SELECT prior_completed_units
            FROM student_requirement_snapshots_old old
            WHERE old.student_id = s.id AND old.requirement_key = 'highway'
        ), 0)
        WHEN r.requirement_key = 'C::night' THEN COALESCE((
            SELECT prior_completed_units
            FROM student_requirement_snapshots_old old
            WHERE old.student_id = s.id AND old.requirement_key = 'night'
        ), 0)
        WHEN r.requirement_key = 'C1::practice' THEN COALESCE((
            SELECT prior_completed_units
            FROM student_requirement_snapshots_old old
            WHERE old.student_id = s.id AND old.requirement_key = 'practice'
        ), 0)
        WHEN r.requirement_key = 'C1::rural' THEN COALESCE((
            SELECT prior_completed_units
            FROM student_requirement_snapshots_old old
            WHERE old.student_id = s.id AND old.requirement_key = 'rural'
        ), 0)
        WHEN r.requirement_key = 'C1::highway' THEN COALESCE((
            SELECT prior_completed_units
            FROM student_requirement_snapshots_old old
            WHERE old.student_id = s.id AND old.requirement_key = 'highway'
        ), 0)
        WHEN r.requirement_key = 'C1::night' THEN COALESCE((
            SELECT prior_completed_units
            FROM student_requirement_snapshots_old old
            WHERE old.student_id = s.id AND old.requirement_key = 'night'
        ), 0)
        ELSE 0
    END,
    r.phase_label
FROM students s
JOIN license_class_templates t ON t.id = s.template_id
JOIN license_template_requirements r ON r.template_id = t.id
WHERE t.code IN ('C+CE', 'C1+C1E');

DROP TABLE IF EXISTS student_requirement_snapshots_old;
