/*
  Warnings:

  - You are about to drop the column `status` on the `domains` table. All the data in the column will be lost.

*/
-- CreateEnum
CREATE TYPE "DomainVerificationStatus" AS ENUM ('PENDING', 'VERIFIED', 'FAILED');

-- CreateEnum
CREATE TYPE "SenderProvider" AS ENUM ('SMTP', 'SES');

-- CreateEnum
CREATE TYPE "SuppressionReason" AS ENUM ('BOUNCE', 'COMPLAINT', 'MANUAL');

-- CreateEnum
CREATE TYPE "EmailEventType" AS ENUM ('SEND', 'DELIVERY', 'BOUNCE', 'COMPLAINT', 'REJECT', 'OPEN', 'CLICK');

-- AlterTable
ALTER TABLE "campaign_logs" ADD COLUMN     "messageId" TEXT;

-- AlterTable
ALTER TABLE "domains" DROP COLUMN "status",
ADD COLUMN     "dkimRecords" JSONB,
ADD COLUMN     "lastCheckedAt" TIMESTAMP(3),
ADD COLUMN     "mailFromDomain" TEXT,
ADD COLUMN     "mailFromMxRecord" TEXT,
ADD COLUMN     "sesIdentityArn" TEXT,
ADD COLUMN     "verificationStatus" "DomainVerificationStatus" NOT NULL DEFAULT 'PENDING',
ADD COLUMN     "verifiedAt" TIMESTAMP(3);

-- AlterTable
ALTER TABLE "senders" ADD COLUMN     "domainId" TEXT,
ADD COLUMN     "provider" "SenderProvider" NOT NULL DEFAULT 'SMTP',
ALTER COLUMN "smtpHost" DROP NOT NULL,
ALTER COLUMN "smtpPort" DROP NOT NULL,
ALTER COLUMN "smtpUser" DROP NOT NULL,
ALTER COLUMN "smtpPass" DROP NOT NULL;

-- CreateTable
CREATE TABLE "suppressed_emails" (
    "id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "reason" "SuppressionReason" NOT NULL,
    "source" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "email_events" (
    "id" TEXT NOT NULL,
    "type" "EmailEventType" NOT NULL,
    "messageId" TEXT NOT NULL,
    "payload" JSONB NOT NULL,
    "campaignId" TEXT,
    "contactId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "suppressed_emails_email_key" ON "suppressed_emails"("email");

-- CreateIndex
CREATE INDEX "email_events_campaignId_type_idx" ON "email_events"("campaignId", "type");

-- CreateIndex
CREATE INDEX "email_events_messageId_idx" ON "email_events"("messageId");

-- CreateIndex
CREATE INDEX "campaign_logs_messageId_idx" ON "campaign_logs"("messageId");

-- AddForeignKey
ALTER TABLE "senders" ADD CONSTRAINT "senders_domainId_fkey" FOREIGN KEY ("domainId") REFERENCES "domains"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_events" ADD CONSTRAINT "email_events_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "campaigns"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_events" ADD CONSTRAINT "email_events_contactId_fkey" FOREIGN KEY ("contactId") REFERENCES "contacts"("id") ON DELETE SET NULL ON UPDATE CASCADE;
