-- CreateEnum
CREATE TYPE "AttemptStatus" AS ENUM ('in_progress', 'completed');

-- CreateEnum
CREATE TYPE "CategoryProgressStatus" AS ENUM ('in_progress', 'completed');

-- CreateTable
CREATE TABLE "categories" (
    "id" VARCHAR(26) NOT NULL,
    "name" VARCHAR(255) NOT NULL,
    "correct_value" INTEGER NOT NULL,
    "incorrect_value" INTEGER NOT NULL,
    "unanswered_value" INTEGER NOT NULL,
    "duration" INTEGER NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "categories_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "questions" (
    "id" VARCHAR(26) NOT NULL,
    "category_id" TEXT NOT NULL,
    "question_text" TEXT,
    "img_path" VARCHAR(255),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "questions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "answers" (
    "id" VARCHAR(26) NOT NULL,
    "question_id" TEXT NOT NULL,
    "answer_text" TEXT,
    "img_path" VARCHAR(255),
    "is_correct" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "answers_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "exams" (
    "id" VARCHAR(26) NOT NULL,
    "name" VARCHAR(255) NOT NULL,
    "exam_code" VARCHAR(50) NOT NULL,
    "end_time" TIMESTAMPTZ(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "exams_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "exam_categories" (
    "exam_id" TEXT NOT NULL,
    "category_id" TEXT NOT NULL,
    "order" INTEGER NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "exam_categories_pkey" PRIMARY KEY ("exam_id","category_id")
);

-- CreateTable
CREATE TABLE "user_exam_attempts" (
    "id" VARCHAR(26) NOT NULL,
    "user_id" VARCHAR(26) NOT NULL,
    "exam_id" TEXT NOT NULL,
    "start_time" TIMESTAMPTZ(3),
    "end_time" TIMESTAMPTZ(3),
    "total_score" INTEGER,
    "leaving_page" INTEGER NOT NULL DEFAULT 0,
    "status" "AttemptStatus" NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "user_exam_attempts_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "user_exam_category_progress" (
    "id" VARCHAR(26) NOT NULL,
    "user_exam_attempt_id" TEXT NOT NULL,
    "category_id" TEXT NOT NULL,
    "status" "CategoryProgressStatus" NOT NULL,
    "start_time" TIMESTAMPTZ(3),
    "end_time" TIMESTAMPTZ(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "user_exam_category_progress_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "user_exam_progress" (
    "id" VARCHAR(26) NOT NULL,
    "user_exam_attempt_id" TEXT NOT NULL,
    "category_id" TEXT NOT NULL,
    "answers" JSONB,
    "flagged_questions" JSONB,
    "current_question" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "user_exam_progress_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "questions_category_id_idx" ON "questions"("category_id");

-- CreateIndex
CREATE INDEX "answers_question_id_idx" ON "answers"("question_id");

-- CreateIndex
CREATE UNIQUE INDEX "exams_exam_code_key" ON "exams"("exam_code");

-- CreateIndex
CREATE INDEX "exam_categories_exam_id_order_idx" ON "exam_categories"("exam_id", "order");

-- CreateIndex
CREATE INDEX "user_exam_attempts_exam_id_status_idx" ON "user_exam_attempts"("exam_id", "status");

-- CreateIndex
CREATE INDEX "user_exam_attempts_user_id_created_at_idx" ON "user_exam_attempts"("user_id", "created_at");

-- CreateIndex
CREATE UNIQUE INDEX "user_exam_attempts_user_id_exam_id_key" ON "user_exam_attempts"("user_id", "exam_id");

-- CreateIndex
CREATE INDEX "user_exam_category_progress_user_exam_attempt_id_status_idx" ON "user_exam_category_progress"("user_exam_attempt_id", "status");

-- CreateIndex
CREATE UNIQUE INDEX "user_exam_category_progress_user_exam_attempt_id_category_i_key" ON "user_exam_category_progress"("user_exam_attempt_id", "category_id");

-- CreateIndex
CREATE UNIQUE INDEX "user_exam_progress_user_exam_attempt_id_category_id_key" ON "user_exam_progress"("user_exam_attempt_id", "category_id");

-- AddForeignKey
ALTER TABLE "questions" ADD CONSTRAINT "questions_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "categories"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "answers" ADD CONSTRAINT "answers_question_id_fkey" FOREIGN KEY ("question_id") REFERENCES "questions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "exam_categories" ADD CONSTRAINT "exam_categories_exam_id_fkey" FOREIGN KEY ("exam_id") REFERENCES "exams"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "exam_categories" ADD CONSTRAINT "exam_categories_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "categories"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_exam_attempts" ADD CONSTRAINT "user_exam_attempts_exam_id_fkey" FOREIGN KEY ("exam_id") REFERENCES "exams"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_exam_category_progress" ADD CONSTRAINT "user_exam_category_progress_user_exam_attempt_id_fkey" FOREIGN KEY ("user_exam_attempt_id") REFERENCES "user_exam_attempts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_exam_category_progress" ADD CONSTRAINT "user_exam_category_progress_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "categories"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_exam_progress" ADD CONSTRAINT "user_exam_progress_user_exam_attempt_id_fkey" FOREIGN KEY ("user_exam_attempt_id") REFERENCES "user_exam_attempts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_exam_progress" ADD CONSTRAINT "user_exam_progress_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "categories"("id") ON DELETE CASCADE ON UPDATE CASCADE;
