-- CreateTable
CREATE TABLE "User" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "email" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slackUserId" TEXT,
    "role" TEXT NOT NULL DEFAULT 'USER',
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "systemPromptAddition" TEXT,
    "githubToken" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    "lastActiveAt" DATETIME
);

-- CreateTable
CREATE TABLE "Secret" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "encryptedValue" TEXT NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "Secret_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "BotSession" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "threadTs" TEXT NOT NULL,
    "channel" TEXT NOT NULL,
    "sessionId" TEXT,
    "workdir" TEXT,
    "containerName" TEXT,
    "model" TEXT NOT NULL DEFAULT 'opus',
    "provider" TEXT NOT NULL DEFAULT 'direct',
    "summary" TEXT,
    "persistUntil" DATETIME,
    "totalCost" REAL NOT NULL DEFAULT 0,
    "totalInput" INTEGER NOT NULL DEFAULT 0,
    "totalOutput" INTEGER NOT NULL DEFAULT 0,
    "messageCount" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "lastActivity" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "BotSession_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Deployment" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "authMode" TEXT NOT NULL,
    "workspacesDir" TEXT NOT NULL,
    "reposDir" TEXT NOT NULL DEFAULT '/opt/gemmbot/repos',
    "baseDomain" TEXT NOT NULL,
    "features" TEXT NOT NULL DEFAULT '{}',
    "dockerMemory" TEXT NOT NULL DEFAULT '4g',
    "dockerCpus" TEXT NOT NULL DEFAULT '2',
    "idleTimeoutBusinessHours" INTEGER NOT NULL DEFAULT 24,
    "warning1HoursRemaining" INTEGER NOT NULL DEFAULT 12,
    "warning2HoursRemaining" INTEGER NOT NULL DEFAULT 4,
    "maxPersistDays" INTEGER NOT NULL DEFAULT 30,
    "defaultPersistDays" INTEGER NOT NULL DEFAULT 7,
    "secretDropoffPort" INTEGER,
    "githubAppId" TEXT,
    "githubAppInstallId" TEXT,
    "gitUserName" TEXT NOT NULL DEFAULT 'Gemmbot',
    "gitUserEmail" TEXT NOT NULL DEFAULT 'gemmbot[bot]@users.noreply.github.com',
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "ExternalRepoAccess" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "sessionId" TEXT NOT NULL,
    "installationId" TEXT NOT NULL,
    "repoFullName" TEXT NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "ExternalRepoAccess_sessionId_fkey" FOREIGN KEY ("sessionId") REFERENCES "BotSession" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Account" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "providerAccountId" TEXT NOT NULL,
    "refresh_token" TEXT,
    "access_token" TEXT,
    "expires_at" INTEGER,
    "token_type" TEXT,
    "scope" TEXT,
    "id_token" TEXT,
    "session_state" TEXT,
    CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "AuthSession" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "sessionToken" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "expires" DATETIME NOT NULL,
    CONSTRAINT "AuthSession_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "VerificationToken" (
    "identifier" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expires" DATETIME NOT NULL
);

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

-- CreateIndex
CREATE UNIQUE INDEX "User_slackUserId_key" ON "User"("slackUserId");

-- CreateIndex
CREATE UNIQUE INDEX "Secret_userId_key_key" ON "Secret"("userId", "key");

-- CreateIndex
CREATE UNIQUE INDEX "BotSession_threadTs_key" ON "BotSession"("threadTs");

-- CreateIndex
CREATE INDEX "BotSession_userId_idx" ON "BotSession"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "Deployment_name_key" ON "Deployment"("name");

-- CreateIndex
CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");

-- CreateIndex
CREATE UNIQUE INDEX "AuthSession_sessionToken_key" ON "AuthSession"("sessionToken");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token");
