-- AlterTable
ALTER TABLE `card` ADD COLUMN `externalMessageId` VARCHAR(191) NULL;

-- CreateTable
CREATE TABLE `IntegrationSettings` (
    `id` VARCHAR(191) NOT NULL DEFAULT 'singleton',
    `microsoftClientId` VARCHAR(191) NULL,
    `microsoftClientSecret` TEXT NULL,
    `microsoftRedirectUri` VARCHAR(191) NULL,
    `updatedAt` DATETIME(3) NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `MailAccount` (
    `id` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `boardId` VARCHAR(191) NOT NULL,
    `provider` VARCHAR(191) NOT NULL DEFAULT 'MICROSOFT',
    `email` VARCHAR(191) NOT NULL,
    `accessToken` TEXT NOT NULL,
    `refreshToken` TEXT NOT NULL,
    `tokenExpiresAt` DATETIME(3) NOT NULL,
    `deltaLink` TEXT NULL,
    `lastSyncedAt` DATETIME(3) NULL,
    `lastSyncError` TEXT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `MailAccount_userId_key`(`userId`),
    UNIQUE INDEX `MailAccount_boardId_key`(`boardId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateIndex
CREATE UNIQUE INDEX `Card_boardId_externalMessageId_key` ON `Card`(`boardId`, `externalMessageId`);

-- AddForeignKey
ALTER TABLE `MailAccount` ADD CONSTRAINT `MailAccount_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `MailAccount` ADD CONSTRAINT `MailAccount_boardId_fkey` FOREIGN KEY (`boardId`) REFERENCES `Board`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
