CREATE TABLE "BackgroundJobState" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "jobName" TEXT NOT NULL,
    "lockedBy" TEXT NOT NULL,
    "lockedUntil" TIMESTAMP(3),
    "runs" INTEGER NOT NULL DEFAULT 0,
    "lastRunStartedAt" TIMESTAMP(3),
    "lastRunCompletedAt" TIMESTAMP(3),
    "lastRunStatus" TEXT,
    "lastRunSummaryJson" JSONB,
    "lastError" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    CONSTRAINT "BackgroundJobState_jobName_key" UNIQUE ("jobName")
);

CREATE INDEX "BackgroundJobState_jobName_idx" ON "BackgroundJobState"("jobName");

CREATE INDEX "CampaignRecipient_campaignId_sendStatus_nextRetryAt_idx" ON "CampaignRecipient"("campaignId", "sendStatus", "nextRetryAt");
CREATE INDEX "CampaignRecipient_campaignId_sendStatus_idx" ON "CampaignRecipient"("campaignId", "sendStatus");
CREATE INDEX "BroadcastCampaign_tenantId_status_idx" ON "BroadcastCampaign"("tenantId", "status");
CREATE INDEX "BroadcastCampaign_status_scheduledAt_idx" ON "BroadcastCampaign"("status", "scheduledAt");
