-- CreateTable
CREATE TABLE "User" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "email" TEXT NOT NULL,
    "name" TEXT,
    "passwordHash" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "ApiKey" (
    "key" TEXT NOT NULL PRIMARY KEY,
    "expires" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "ownerId" INTEGER NOT NULL,
    CONSTRAINT "ApiKey_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Page" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
);

-- CreateTable
CREATE TABLE "Concept" (
    "name" TEXT NOT NULL PRIMARY KEY
);

-- CreateTable
CREATE TABLE "Exercise" (
    "name" TEXT NOT NULL PRIMARY KEY,
    "isGroup" BOOLEAN NOT NULL,
    "parentName" TEXT,
    "graderConfig" TEXT NOT NULL,
    "graderType" TEXT NOT NULL,
    CONSTRAINT "Exercise_parentName_fkey" FOREIGN KEY ("parentName") REFERENCES "Exercise" ("name") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ExerciseInstance" (
    "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "exerciseName" TEXT NOT NULL,
    "userId" INTEGER NOT NULL,
    "timeSubmitted" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "timeClaimed" DATETIME NOT NULL,
    "timeoutSeconds" INTEGER NOT NULL,
    "timeCompleted" DATETIME,
    "needsGrading" BOOLEAN NOT NULL,
    "gradingProgress" TEXT NOT NULL,
    "gradingStatus" TEXT NOT NULL,
    "gradingVerdict" TEXT NOT NULL,
    "gradingFeedback" TEXT NOT NULL,
    CONSTRAINT "ExerciseInstance_exerciseName_fkey" FOREIGN KEY ("exerciseName") REFERENCES "Exercise" ("name") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "ExerciseInstance_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ExerciseConceptSat" (
    "exerciseName" TEXT NOT NULL,
    "conceptName" TEXT NOT NULL,
    "satisfiesFactor" REAL NOT NULL,

    PRIMARY KEY ("exerciseName", "conceptName"),
    CONSTRAINT "ExerciseConceptSat_exerciseName_fkey" FOREIGN KEY ("exerciseName") REFERENCES "Exercise" ("name") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "ExerciseConceptSat_conceptName_fkey" FOREIGN KEY ("conceptName") REFERENCES "Concept" ("name") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
