-- CreateTable
CREATE TABLE "DeployTarget" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "host" TEXT NOT NULL,
    "sshPort" INTEGER NOT NULL DEFAULT 22,
    "sshUser" TEXT NOT NULL DEFAULT 'root',
    "authMode" TEXT NOT NULL,
    "authBlob" TEXT NOT NULL,
    "composePath" TEXT NOT NULL DEFAULT '/opt/{name}/docker-compose.yml',
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "DeployHistory" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectId" TEXT NOT NULL,
    "targetId" TEXT,
    "imageTag" TEXT NOT NULL,
    "commitHash" TEXT,
    "status" TEXT NOT NULL,
    "startedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "finishedAt" DATETIME,
    "errorMsg" TEXT,
    "logBlob" TEXT,
    CONSTRAINT "DeployHistory_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Project" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "path" TEXT NOT NULL,
    "source" TEXT NOT NULL,
    "gitRemote" TEXT,
    "branch" TEXT,
    "version" TEXT,
    "lastCommitHash" TEXT,
    "lastCommitMessage" TEXT,
    "lastCommitTime" DATETIME,
    "notes" TEXT,
    "tags" TEXT,
    "priority" INTEGER NOT NULL DEFAULT 0,
    "deployTargetId" TEXT,
    "envVarsEncrypted" TEXT,
    "composeSource" TEXT NOT NULL DEFAULT 'repo',
    "composeInline" TEXT,
    "deployedVersion" TEXT,
    "deployedAt" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "Project_deployTargetId_fkey" FOREIGN KEY ("deployTargetId") REFERENCES "DeployTarget" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Project" ("branch", "createdAt", "gitRemote", "id", "lastCommitHash", "lastCommitMessage", "lastCommitTime", "name", "notes", "path", "priority", "source", "tags", "updatedAt", "version") SELECT "branch", "createdAt", "gitRemote", "id", "lastCommitHash", "lastCommitMessage", "lastCommitTime", "name", "notes", "path", "priority", "source", "tags", "updatedAt", "version" FROM "Project";
DROP TABLE "Project";
ALTER TABLE "new_Project" RENAME TO "Project";
CREATE UNIQUE INDEX "Project_name_key" ON "Project"("name");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

-- CreateIndex
CREATE UNIQUE INDEX "DeployTarget_name_key" ON "DeployTarget"("name");

-- CreateIndex
CREATE INDEX "DeployHistory_projectId_startedAt_idx" ON "DeployHistory"("projectId", "startedAt");
