-- +goose Up

CREATE TYPE question_topic AS ENUM (
    'place_value',
    'arithmetic',
    'negative_numbers',
    'bidmas',
    'fractions',
    'algebra',
    'geometry',
    'data'
);

CREATE TYPE question_difficulty AS ENUM ('easy', 'medium', 'hard');

ALTER TABLE questions
    ADD COLUMN topic question_topic,
    ADD COLUMN difficulty question_difficulty;

UPDATE questions
SET topic = CASE BTRIM(COALESCE(subject, ''))
    WHEN 'Place Value' THEN 'place_value'::question_topic
    WHEN 'Arithmetic' THEN 'arithmetic'::question_topic
    WHEN 'Negative Numbers' THEN 'negative_numbers'::question_topic
    WHEN 'BIDMAS' THEN 'bidmas'::question_topic
    WHEN 'Fractions' THEN 'fractions'::question_topic
    WHEN 'Algebra' THEN 'algebra'::question_topic
    WHEN 'Geometry' THEN 'geometry'::question_topic
    WHEN 'Data' THEN 'data'::question_topic
    ELSE NULL
END
WHERE topic IS NULL;

UPDATE questions AS q
SET difficulty = seeded.difficulty::question_difficulty
FROM (
    VALUES
        (1001, 'easy'),
        (1002, 'medium'),
        (1003, 'medium'),
        (1004, 'hard'),
        (1101, 'easy'),
        (1102, 'medium'),
        (1103, 'hard'),
        (1104, 'medium'),
        (1105, 'medium'),
        (1106, 'hard'),
        (1201, 'easy'),
        (1202, 'medium'),
        (1203, 'hard'),
        (1301, 'easy'),
        (1302, 'medium'),
        (1303, 'hard'),
        (1401, 'easy'),
        (1402, 'medium'),
        (1403, 'hard'),
        (1411, 'easy'),
        (1412, 'easy'),
        (1413, 'medium'),
        (1414, 'medium'),
        (1415, 'medium'),
        (1416, 'hard'),
        (1421, 'easy'),
        (1422, 'medium'),
        (1423, 'hard'),
        (1501, 'easy'),
        (1502, 'medium'),
        (1503, 'hard'),
        (1511, 'easy'),
        (1512, 'medium'),
        (1513, 'hard'),
        (1601, 'easy'),
        (1602, 'medium'),
        (1603, 'hard'),
        (1611, 'easy'),
        (1612, 'medium'),
        (1613, 'hard'),
        (1701, 'easy'),
        (1702, 'medium'),
        (1703, 'easy'),
        (1711, 'medium'),
        (1712, 'hard'),
        (1801, 'easy'),
        (1802, 'medium'),
        (1803, 'medium'),
        (1804, 'hard'),
        (1805, 'hard')
) AS seeded(id, difficulty)
WHERE q.id = seeded.id
  AND q.difficulty IS NULL;

ALTER TABLE assignments
    ALTER COLUMN pass_threshold SET DEFAULT 6.00;

UPDATE assignments
SET pass_threshold = 6.00;

ALTER TABLE assignment_assignees
    ALTER COLUMN pass_threshold SET DEFAULT 6.00;

UPDATE assignment_assignees
SET pass_threshold = 6.00;

-- +goose Down

UPDATE assignment_assignees
SET pass_threshold = 8.00;

ALTER TABLE assignment_assignees
    ALTER COLUMN pass_threshold SET DEFAULT 8.00;

UPDATE assignments
SET pass_threshold = 8.00;

ALTER TABLE assignments
    ALTER COLUMN pass_threshold SET DEFAULT 8.00;

ALTER TABLE questions
    DROP COLUMN IF EXISTS difficulty,
    DROP COLUMN IF EXISTS topic;

DROP TYPE IF EXISTS question_difficulty;
DROP TYPE IF EXISTS question_topic;
