-- CreateTable
CREATE TABLE "domain_configs" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "universityId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "domain" TEXT NOT NULL,
    "subdomain" TEXT,
    "sslStatus" TEXT NOT NULL DEFAULT 'PENDING',
    "sslExpiryDate" DATETIME,
    "dnsStatus" TEXT NOT NULL DEFAULT 'PENDING',
    "isActive" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "domain_configs_universityId_fkey" FOREIGN KEY ("universityId") REFERENCES "universities" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ssl_configs" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "domainId" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "certificatePath" TEXT,
    "privateKeyPath" TEXT,
    "autoRenewal" BOOLEAN NOT NULL DEFAULT true,
    "renewalThreshold" INTEGER NOT NULL DEFAULT 30,
    "lastRenewalDate" DATETIME,
    "nextRenewalDate" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "ssl_configs_domainId_fkey" FOREIGN KEY ("domainId") REFERENCES "domain_configs" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "dns_configs" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "domainId" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "apiKey" TEXT,
    "zoneId" TEXT,
    "recordType" TEXT NOT NULL,
    "recordValue" TEXT NOT NULL,
    "ttl" INTEGER NOT NULL DEFAULT 300,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "dns_configs_domainId_fkey" FOREIGN KEY ("domainId") REFERENCES "domain_configs" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "domain_analytics" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "domainId" TEXT NOT NULL,
    "uptime" REAL NOT NULL,
    "responseTime" INTEGER NOT NULL,
    "sslStatus" TEXT NOT NULL,
    "dnsStatus" TEXT NOT NULL,
    "checkedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "domain_analytics_domainId_fkey" FOREIGN KEY ("domainId") REFERENCES "domain_configs" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "deployment_configs" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "universityId" TEXT NOT NULL,
    "environment" TEXT NOT NULL,
    "version" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'PENDING',
    "deploymentType" TEXT NOT NULL DEFAULT 'FULL',
    "startTime" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "endTime" DATETIME,
    "logs" JSONB NOT NULL,
    "metadata" JSONB NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "deployment_configs_universityId_fkey" FOREIGN KEY ("universityId") REFERENCES "universities" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "environment_configs" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "domain" TEXT NOT NULL,
    "databaseUrl" TEXT NOT NULL,
    "apiKeys" JSONB NOT NULL,
    "features" JSONB NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "cdn_configs" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "universityId" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "bucketName" TEXT,
    "region" TEXT,
    "accessKey" TEXT,
    "secretKey" TEXT,
    "domain" TEXT NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "cdn_configs_universityId_fkey" FOREIGN KEY ("universityId") REFERENCES "universities" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "cdn_assets" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "universityId" TEXT NOT NULL,
    "originalPath" TEXT NOT NULL,
    "cdnUrl" TEXT NOT NULL,
    "optimizedUrls" JSONB NOT NULL,
    "metadata" JSONB NOT NULL,
    "uploadedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "cdn_assets_universityId_fkey" FOREIGN KEY ("universityId") REFERENCES "universities" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "cdn_assets_universityId_fkey" FOREIGN KEY ("universityId") REFERENCES "cdn_configs" ("universityId") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_universities" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "slug" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "shortName" TEXT,
    "domain" TEXT,
    "subdomain" TEXT,
    "branding" JSONB NOT NULL,
    "contact" JSONB NOT NULL,
    "features" JSONB NOT NULL,
    "ai" JSONB NOT NULL,
    "isMultiBranch" BOOLEAN NOT NULL DEFAULT false,
    "parentUniversityId" TEXT,
    "branchType" TEXT,
    "status" TEXT NOT NULL DEFAULT 'SETUP',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "universities_parentUniversityId_fkey" FOREIGN KEY ("parentUniversityId") REFERENCES "universities" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_universities" ("ai", "branding", "contact", "createdAt", "domain", "features", "id", "name", "shortName", "slug", "status", "subdomain", "updatedAt") SELECT "ai", "branding", "contact", "createdAt", "domain", "features", "id", "name", "shortName", "slug", "status", "subdomain", "updatedAt" FROM "universities";
DROP TABLE "universities";
ALTER TABLE "new_universities" RENAME TO "universities";
CREATE UNIQUE INDEX "universities_slug_key" ON "universities"("slug");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

-- CreateIndex
CREATE UNIQUE INDEX "ssl_configs_domainId_key" ON "ssl_configs"("domainId");

-- CreateIndex
CREATE UNIQUE INDEX "dns_configs_domainId_key" ON "dns_configs"("domainId");

-- CreateIndex
CREATE UNIQUE INDEX "cdn_configs_universityId_key" ON "cdn_configs"("universityId");
