/*
  Warnings:

  - Added the required column `workspaceId` to the `Inbox` table without a default value. This is not possible if the table is not empty.

*/
-- CreateTable
CREATE TABLE "Workspace" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

INSERT INTO "Workspace" ("id", "name", "createdAt", "updatedAt")
VALUES ('ws_default', 'default', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

-- CreateTable
CREATE TABLE "WarmupCampaign" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "workspaceId" TEXT NOT NULL,
    "primaryMailboxId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "recipe" TEXT NOT NULL DEFAULT 'grow',
    "status" TEXT NOT NULL DEFAULT 'draft',
    "durationDays" INTEGER NOT NULL DEFAULT 30,
    "minEmailsPerDay" INTEGER NOT NULL DEFAULT 1,
    "maxEmailsPerDay" INTEGER NOT NULL DEFAULT 40,
    "replyRatePercent" INTEGER NOT NULL DEFAULT 30,
    "maintenanceVolumePercent" INTEGER NOT NULL DEFAULT 20,
    "timezone" TEXT NOT NULL DEFAULT 'America/Vancouver',
    "sendWindowStart" TEXT NOT NULL DEFAULT '08:00',
    "sendWindowEnd" TEXT NOT NULL DEFAULT '18:00',
    "customSchedule" TEXT,
    "startedAt" DATETIME,
    "endsAt" DATETIME,
    "currentDay" INTEGER NOT NULL DEFAULT 0,
    "pausedFromStatus" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "WarmupCampaign_workspaceId_fkey" FOREIGN KEY ("workspaceId") REFERENCES "Workspace" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "WarmupCampaign_primaryMailboxId_fkey" FOREIGN KEY ("primaryMailboxId") REFERENCES "Inbox" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "CampaignSatellite" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "campaignId" TEXT NOT NULL,
    "satelliteMailboxId" TEXT NOT NULL,
    "weight" INTEGER NOT NULL DEFAULT 1,
    "active" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "CampaignSatellite_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "WarmupCampaign" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "CampaignSatellite_satelliteMailboxId_fkey" FOREIGN KEY ("satelliteMailboxId") REFERENCES "Inbox" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "DailySchedule" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "campaignId" TEXT NOT NULL,
    "dayIndex" INTEGER NOT NULL,
    "date" DATETIME,
    "plannedSends" INTEGER NOT NULL,
    "plannedReplies" INTEGER NOT NULL,
    "actualSends" INTEGER NOT NULL DEFAULT 0,
    "actualReplies" INTEGER NOT NULL DEFAULT 0,
    "spamDetected" INTEGER NOT NULL DEFAULT 0,
    "spamRescued" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "DailySchedule_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "WarmupCampaign" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Inbox" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "workspaceId" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "smtpHost" TEXT NOT NULL,
    "smtpPort" INTEGER NOT NULL,
    "imapHost" TEXT NOT NULL,
    "imapPort" INTEGER NOT NULL,
    "username" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "role" TEXT NOT NULL DEFAULT 'unassigned',
    "provider" TEXT NOT NULL DEFAULT 'custom',
    "status" TEXT NOT NULL DEFAULT 'active',
    "dailyLimit" INTEGER NOT NULL DEFAULT 40,
    "currentRamp" INTEGER NOT NULL DEFAULT 5,
    "industry" TEXT NOT NULL DEFAULT 'SaaS',
    "senderName" TEXT,
    "companyName" TEXT,
    "lastError" TEXT,
    "errorAt" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "Inbox_workspaceId_fkey" FOREIGN KEY ("workspaceId") REFERENCES "Workspace" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_Inbox" ("createdAt", "currentRamp", "dailyLimit", "email", "errorAt", "id", "imapHost", "imapPort", "industry", "lastError", "password", "smtpHost", "smtpPort", "status", "updatedAt", "username", "workspaceId") SELECT "createdAt", "currentRamp", "dailyLimit", "email", "errorAt", "id", "imapHost", "imapPort", "industry", "lastError", "password", "smtpHost", "smtpPort", "status", "updatedAt", "username", 'ws_default' FROM "Inbox";
DROP TABLE "Inbox";
ALTER TABLE "new_Inbox" RENAME TO "Inbox";
CREATE INDEX "Inbox_status_idx" ON "Inbox"("status");
CREATE INDEX "Inbox_workspaceId_role_idx" ON "Inbox"("workspaceId", "role");
CREATE UNIQUE INDEX "Inbox_workspaceId_email_key" ON "Inbox"("workspaceId", "email");
CREATE TABLE "new_WarmupLog" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "campaignId" TEXT,
    "senderId" TEXT NOT NULL,
    "receiverId" TEXT NOT NULL,
    "messageId" TEXT,
    "inReplyTo" TEXT,
    "subject" TEXT NOT NULL,
    "body" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'sent',
    "rescuedFromSpam" BOOLEAN NOT NULL DEFAULT false,
    "validationResult" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "WarmupLog_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "WarmupCampaign" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "WarmupLog_senderId_fkey" FOREIGN KEY ("senderId") REFERENCES "Inbox" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "WarmupLog_receiverId_fkey" FOREIGN KEY ("receiverId") REFERENCES "Inbox" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_WarmupLog" ("body", "createdAt", "id", "inReplyTo", "messageId", "receiverId", "rescuedFromSpam", "senderId", "status", "subject", "updatedAt") SELECT "body", "createdAt", "id", "inReplyTo", "messageId", "receiverId", "rescuedFromSpam", "senderId", "status", "subject", "updatedAt" FROM "WarmupLog";
DROP TABLE "WarmupLog";
ALTER TABLE "new_WarmupLog" RENAME TO "WarmupLog";
CREATE INDEX "WarmupLog_senderId_status_idx" ON "WarmupLog"("senderId", "status");
CREATE INDEX "WarmupLog_receiverId_status_idx" ON "WarmupLog"("receiverId", "status");
CREATE INDEX "WarmupLog_messageId_idx" ON "WarmupLog"("messageId");
CREATE INDEX "WarmupLog_campaignId_idx" ON "WarmupLog"("campaignId");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

-- CreateIndex
CREATE UNIQUE INDEX "Workspace_name_key" ON "Workspace"("name");

-- CreateIndex
CREATE INDEX "WarmupCampaign_workspaceId_status_idx" ON "WarmupCampaign"("workspaceId", "status");

-- CreateIndex
CREATE INDEX "WarmupCampaign_primaryMailboxId_status_idx" ON "WarmupCampaign"("primaryMailboxId", "status");

-- CreateIndex
CREATE INDEX "CampaignSatellite_satelliteMailboxId_idx" ON "CampaignSatellite"("satelliteMailboxId");

-- CreateIndex
CREATE UNIQUE INDEX "CampaignSatellite_campaignId_satelliteMailboxId_key" ON "CampaignSatellite"("campaignId", "satelliteMailboxId");

-- CreateIndex
CREATE INDEX "DailySchedule_campaignId_idx" ON "DailySchedule"("campaignId");

-- CreateIndex
CREATE UNIQUE INDEX "DailySchedule_campaignId_dayIndex_key" ON "DailySchedule"("campaignId", "dayIndex");
