-- AlterTable
ALTER TABLE `confessions` ADD COLUMN `source` ENUM('USER', 'BOT') NOT NULL DEFAULT 'USER';

-- CreateTable
CREATE TABLE `rooms` (
    `id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `slug` VARCHAR(191) NOT NULL,
    `description` TEXT NULL,
    `introArticle` TEXT NULL,
    `coverImage` VARCHAR(191) NULL,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `sortOrder` INTEGER NOT NULL DEFAULT 0,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `rooms_slug_key`(`slug`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `bot_schedules` (
    `id` VARCHAR(191) NOT NULL,
    `taskType` ENUM('CONFESSION', 'QUOTE', 'ROOM_ARTICLE') NOT NULL,
    `runDays` VARCHAR(191) NOT NULL,
    `runTime` VARCHAR(191) NOT NULL,
    `categoryId` VARCHAR(191) NULL,
    `roomId` VARCHAR(191) NULL,
    `autoPublish` BOOLEAN NOT NULL DEFAULT false,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `lastRunAt` DATETIME(3) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `bot_schedules_isActive_idx`(`isActive`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `bot_job_logs` (
    `id` VARCHAR(191) NOT NULL,
    `scheduleId` VARCHAR(191) NOT NULL,
    `runAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `status` ENUM('SUCCESS', 'FAILED') NOT NULL,
    `generatedCount` INTEGER NOT NULL DEFAULT 0,
    `errorMessage` TEXT NULL,
    `rawResponse` JSON NULL,

    INDEX `bot_job_logs_scheduleId_runAt_idx`(`scheduleId`, `runAt`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `bot_schedules` ADD CONSTRAINT `bot_schedules_categoryId_fkey` FOREIGN KEY (`categoryId`) REFERENCES `quote_categories`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `bot_schedules` ADD CONSTRAINT `bot_schedules_roomId_fkey` FOREIGN KEY (`roomId`) REFERENCES `rooms`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `bot_job_logs` ADD CONSTRAINT `bot_job_logs_scheduleId_fkey` FOREIGN KEY (`scheduleId`) REFERENCES `bot_schedules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
