CREATE TABLE "BackgroundJobState" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "jobName" TEXT NOT NULL,
    "lockedBy" TEXT NOT NULL,
    "lockedUntil" DATETIME,
    "runs" INTEGER NOT NULL DEFAULT 0,
    "lastRunStartedAt" DATETIME,
    "lastRunCompletedAt" DATETIME,
    "lastRunStatus" TEXT,
    "lastRunSummaryJson" JSONB,
    "lastError" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME 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");
