-- CreateEnum
CREATE TYPE "PreviewStatus" AS ENUM ('PROVISIONING', 'BUILDING', 'RUNNING', 'FAILED', 'STOPPED', 'IGNORED');

-- CreateEnum
CREATE TYPE "JobType" AS ENUM ('DEPLOY', 'STOP', 'INACTIVITY_STOP');

-- CreateEnum
CREATE TYPE "JobStatus" AS ENUM ('PENDING', 'RUNNING', 'DONE', 'FAILED');

-- CreateTable
CREATE TABLE "User" (
    "id" SERIAL NOT NULL,
    "username" VARCHAR(128) NOT NULL,
    "passwordHash" VARCHAR(256) NOT NULL,
    "isAdmin" BOOLEAN NOT NULL DEFAULT false,
    "isFounder" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "giteaUsername" VARCHAR(128),
    "giteaPAT" TEXT,
    "giteaInstanceUrl" VARCHAR(512),
    "awsAccessKeyId" VARCHAR(256),
    "awsSecretAccessKey" TEXT,
    "awsRegion" VARCHAR(64),

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

-- CreateTable
CREATE TABLE "Session" (
    "id" SERIAL NOT NULL,
    "hash" VARCHAR(512) NOT NULL,
    "userId" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "RepoConfig" (
    "id" SERIAL NOT NULL,
    "userId" INTEGER NOT NULL,
    "repoOwner" VARCHAR(128) NOT NULL,
    "repoName" VARCHAR(128) NOT NULL,
    "isEnabled" BOOLEAN NOT NULL DEFAULT false,
    "giteaWebhookId" VARCHAR(128),
    "denyList" TEXT[],
    "instanceType" VARCHAR(64) NOT NULL DEFAULT 't2.medium',
    "inactivityHours" DOUBLE PRECISION NOT NULL DEFAULT 12,
    "port" INTEGER NOT NULL DEFAULT 3000,
    "envVars" JSONB NOT NULL DEFAULT '{}',
    "useDockerCompose" BOOLEAN NOT NULL DEFAULT false,
    "composeFilePath" VARCHAR(512),
    "aptPackages" TEXT[],
    "setupCommands" TEXT[],
    "buildCommands" TEXT[],
    "postBuildCommands" TEXT[],
    "runCommand" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Preview" (
    "id" SERIAL NOT NULL,
    "repoConfigId" INTEGER NOT NULL,
    "prNumber" INTEGER NOT NULL,
    "prTitle" VARCHAR(512) NOT NULL,
    "commitSha" VARCHAR(64) NOT NULL,
    "instanceId" VARCHAR(64),
    "instanceIp" VARCHAR(64),
    "port" INTEGER NOT NULL DEFAULT 3000,
    "status" "PreviewStatus" NOT NULL DEFAULT 'PROVISIONING',
    "logs" TEXT NOT NULL DEFAULT '',
    "pid" INTEGER,
    "sshPrivateKey" TEXT,
    "sshKeyName" VARCHAR(128),
    "giteaCommentId" INTEGER,
    "lastActivityAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "stoppedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "Job" (
    "id" SERIAL NOT NULL,
    "previewId" INTEGER,
    "type" "JobType" NOT NULL,
    "status" "JobStatus" NOT NULL DEFAULT 'PENDING',
    "payload" JSONB NOT NULL DEFAULT '{}',
    "error" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "startedAt" TIMESTAMP(3),
    "finishedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "WebhookToken" (
    "id" SERIAL NOT NULL,
    "userId" INTEGER NOT NULL,
    "token" VARCHAR(256) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "NoConfigComment" (
    "id" SERIAL NOT NULL,
    "userId" INTEGER NOT NULL,
    "repoOwner" VARCHAR(128) NOT NULL,
    "repoName" VARCHAR(128) NOT NULL,
    "prNumber" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "AdminSettings" (
    "id" INTEGER NOT NULL DEFAULT 1,
    "defaultInstanceType" VARCHAR(64) NOT NULL DEFAULT 't2.medium',
    "maxConcurrentInstancesPerUser" INTEGER NOT NULL DEFAULT 5,
    "logSizeLimitBytes" INTEGER NOT NULL DEFAULT 1048576,
    "previewRetentionDays" INTEGER NOT NULL DEFAULT 30,
    "webhookRateLimitPerMinute" INTEGER NOT NULL DEFAULT 10,
    "contactEmail" VARCHAR(256) NOT NULL DEFAULT '',

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

-- CreateIndex
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");

-- CreateIndex
CREATE UNIQUE INDEX "RepoConfig_repoOwner_repoName_key" ON "RepoConfig"("repoOwner", "repoName");

-- CreateIndex
CREATE UNIQUE INDEX "WebhookToken_userId_key" ON "WebhookToken"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "NoConfigComment_userId_repoOwner_repoName_prNumber_key" ON "NoConfigComment"("userId", "repoOwner", "repoName", "prNumber");

-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RepoConfig" ADD CONSTRAINT "RepoConfig_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Preview" ADD CONSTRAINT "Preview_repoConfigId_fkey" FOREIGN KEY ("repoConfigId") REFERENCES "RepoConfig"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Job" ADD CONSTRAINT "Job_previewId_fkey" FOREIGN KEY ("previewId") REFERENCES "Preview"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "WebhookToken" ADD CONSTRAINT "WebhookToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "NoConfigComment" ADD CONSTRAINT "NoConfigComment_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
