-- CreateTable
CREATE TABLE `streamer` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(191) NOT NULL,
    `platform` ENUM('TWITCH', 'YOUTUBE', 'TIKTOK', 'KICK') NOT NULL,
    `platformUserId` VARCHAR(191) NULL,
    `displayName` VARCHAR(191) NULL,
    `profileImageUrl` VARCHAR(191) NULL,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `lastChecked` DATETIME(3) NULL,
    `lastLiveAt` DATETIME(3) NULL,
    `isCurrentlyLive` BOOLEAN NOT NULL DEFAULT false,
    `currentTitle` VARCHAR(191) NULL,
    `currentGame` VARCHAR(191) NULL,
    `currentViewers` INTEGER NULL,
    `discordBotId` INTEGER NULL,
    `discordChannelId` VARCHAR(191) NULL,
    `customMessage` VARCHAR(191) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `createdBy` INTEGER NOT NULL,

    INDEX `idx_streamer_platform`(`platform`),
    INDEX `idx_streamer_active`(`isActive`),
    INDEX `idx_streamer_live`(`isCurrentlyLive`),
    INDEX `idx_streamer_creator`(`createdBy`),
    UNIQUE INDEX `unique_streamer_platform`(`username`, `platform`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `streamNotification` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `streamerId` INTEGER NOT NULL,
    `platform` ENUM('TWITCH', 'YOUTUBE', 'TIKTOK', 'KICK') NOT NULL,
    `title` VARCHAR(191) NULL,
    `game` VARCHAR(191) NULL,
    `viewers` INTEGER NULL,
    `startedAt` DATETIME(3) NOT NULL,
    `endedAt` DATETIME(3) NULL,
    `notificationSent` BOOLEAN NOT NULL DEFAULT false,
    `discordMessageId` VARCHAR(191) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `idx_notification_streamer`(`streamerId`),
    INDEX `idx_notification_started`(`startedAt`),
    INDEX `idx_notification_sent`(`notificationSent`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `streamer` ADD CONSTRAINT `streamer_createdBy_fkey` FOREIGN KEY (`createdBy`) REFERENCES `user`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `streamer` ADD CONSTRAINT `streamer_discordBotId_fkey` FOREIGN KEY (`discordBotId`) REFERENCES `discordBot`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `streamNotification` ADD CONSTRAINT `streamNotification_streamerId_fkey` FOREIGN KEY (`streamerId`) REFERENCES `streamer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
