-- +goose Up

CREATE TYPE assignment_pass_status AS ENUM ('pending', 'pass', 'no_pass');

ALTER TABLE student_answers
    ADD COLUMN review_needs_attention BOOLEAN NOT NULL DEFAULT FALSE,
    ADD COLUMN review_issue_reason TEXT,
    ADD COLUMN review_correctness_score NUMERIC(4,3),
    ADD COLUMN review_understanding_score NUMERIC(4,3),
    ADD COLUMN review_question_score NUMERIC(4,3),
    ADD COLUMN review_confidence NUMERIC(4,3),
    ADD COLUMN review_tags TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[],
    ADD CONSTRAINT student_answers_review_correctness_score_range_check
        CHECK (review_correctness_score IS NULL OR (review_correctness_score >= 0 AND review_correctness_score <= 1)),
    ADD CONSTRAINT student_answers_review_understanding_score_range_check
        CHECK (review_understanding_score IS NULL OR (review_understanding_score >= 0 AND review_understanding_score <= 1)),
    ADD CONSTRAINT student_answers_review_question_score_range_check
        CHECK (review_question_score IS NULL OR (review_question_score >= 0 AND review_question_score <= 1)),
    ADD CONSTRAINT student_answers_review_confidence_range_check
        CHECK (review_confidence IS NULL OR (review_confidence >= 0 AND review_confidence <= 1));

UPDATE student_answers
SET review_correctness_score = CASE
        WHEN is_correct IS TRUE THEN 1.000
        WHEN is_correct IS FALSE THEN 0.000
        ELSE NULL
    END,
    review_question_score = CASE
        WHEN is_correct IS TRUE THEN 1.000
        WHEN is_correct IS FALSE THEN 0.000
        ELSE NULL
    END
WHERE is_correct IS NOT NULL;

ALTER TABLE assignment_assignees
    ADD COLUMN overall_score NUMERIC(5,2),
    ADD COLUMN pass_threshold NUMERIC(5,2) NOT NULL DEFAULT 8.00,
    ADD COLUMN pass_status assignment_pass_status NOT NULL DEFAULT 'pending',
    ADD CONSTRAINT assignment_assignees_overall_score_range_check
        CHECK (overall_score IS NULL OR (overall_score >= 0 AND overall_score <= 10)),
    ADD CONSTRAINT assignment_assignees_pass_threshold_range_check
        CHECK (pass_threshold >= 0 AND pass_threshold <= 10);

-- +goose Down

ALTER TABLE assignment_assignees
    DROP CONSTRAINT IF EXISTS assignment_assignees_pass_threshold_range_check,
    DROP CONSTRAINT IF EXISTS assignment_assignees_overall_score_range_check,
    DROP COLUMN IF EXISTS pass_status,
    DROP COLUMN IF EXISTS pass_threshold,
    DROP COLUMN IF EXISTS overall_score;

ALTER TABLE student_answers
    DROP CONSTRAINT IF EXISTS student_answers_review_confidence_range_check,
    DROP CONSTRAINT IF EXISTS student_answers_review_question_score_range_check,
    DROP CONSTRAINT IF EXISTS student_answers_review_understanding_score_range_check,
    DROP CONSTRAINT IF EXISTS student_answers_review_correctness_score_range_check,
    DROP COLUMN IF EXISTS review_tags,
    DROP COLUMN IF EXISTS review_confidence,
    DROP COLUMN IF EXISTS review_question_score,
    DROP COLUMN IF EXISTS review_understanding_score,
    DROP COLUMN IF EXISTS review_correctness_score,
    DROP COLUMN IF EXISTS review_issue_reason,
    DROP COLUMN IF EXISTS review_needs_attention;

DROP TYPE IF EXISTS assignment_pass_status;
