/*
  Warnings:

  - You are about to drop the column `createdBy` on the `discordbot` table. All the data in the column will be lost.
  - You are about to drop the column `description` on the `discordbot` table. All the data in the column will be lost.
  - You are about to drop the column `features` on the `discordbot` table. All the data in the column will be lost.
  - You are about to drop the column `webhookUrl` on the `discordbot` table. All the data in the column will be lost.
  - You are about to drop the column `canAccessAdminPanel` on the `user` table. All the data in the column will be lost.
  - You are about to drop the column `canAccessUserPortal` on the `user` table. All the data in the column will be lost.
  - You are about to drop the column `canManageUsers` on the `user` table. All the data in the column will be lost.
  - You are about to drop the column `cfxId` on the `user` table. All the data in the column will be lost.
  - You are about to drop the column `resetPasswordExpires` on the `user` table. All the data in the column will be lost.
  - You are about to drop the column `resetPasswordToken` on the `user` table. All the data in the column will be lost.
  - You are about to drop the column `verificationExpires` on the `user` table. All the data in the column will be lost.
  - You are about to drop the column `verificationToken` on the `user` table. All the data in the column will be lost.
  - You are about to alter the column `role` on the `user` table. The data in that column could be lost. The data in that column will be cast from `Enum(EnumId(2))` to `Enum(EnumId(0))`.

*/
-- DropForeignKey
ALTER TABLE `discordbot` DROP FOREIGN KEY `discordBot_createdBy_fkey`;

-- DropIndex
DROP INDEX `idx_bot_client` ON `discordbot`;

-- DropIndex
DROP INDEX `idx_bot_created` ON `discordbot`;

-- DropIndex
DROP INDEX `User_cfxId_key` ON `user`;

-- DropIndex
DROP INDEX `User_resetPasswordToken_key` ON `user`;

-- DropIndex
DROP INDEX `User_verificationToken_key` ON `user`;

-- DropIndex
DROP INDEX `idx_user_created` ON `user`;

-- AlterTable
ALTER TABLE `discordbot` DROP COLUMN `createdBy`,
    DROP COLUMN `description`,
    DROP COLUMN `features`,
    DROP COLUMN `webhookUrl`,
    ADD COLUMN `clientSecret` TEXT NULL,
    ADD COLUMN `status` VARCHAR(191) NOT NULL DEFAULT 'offline',
    ADD COLUMN `whitelistChannelId` VARCHAR(191) NULL,
    MODIFY `botType` VARCHAR(191) NOT NULL DEFAULT 'general';

-- AlterTable
ALTER TABLE `user` DROP COLUMN `canAccessAdminPanel`,
    DROP COLUMN `canAccessUserPortal`,
    DROP COLUMN `canManageUsers`,
    DROP COLUMN `cfxId`,
    DROP COLUMN `resetPasswordExpires`,
    DROP COLUMN `resetPasswordToken`,
    DROP COLUMN `verificationExpires`,
    DROP COLUMN `verificationToken`,
    ADD COLUMN `isTemporaryPassword` BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN `passwordChangeRequired` BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN `preferredTwoFactorMethod` ENUM('TOTP', 'PUSH', 'BOTH') NOT NULL DEFAULT 'TOTP',
    ADD COLUMN `pushTwoFactorEnabled` BOOLEAN NOT NULL DEFAULT false,
    MODIFY `role` ENUM('VERIAGENT', 'MODERATOR', 'DEVELOPERS', 'ADMIN', 'SUPER_ADMIN') NOT NULL DEFAULT 'VERIAGENT',
    MODIFY `twoFactorBackupCodes` VARCHAR(191) NULL;

-- CreateTable
CREATE TABLE `discordnotificationconfig` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `serverId` VARCHAR(191) NOT NULL,
    `serverName` VARCHAR(191) NOT NULL,
    `channelId` VARCHAR(191) NOT NULL,
    `channelName` VARCHAR(191) NOT NULL,
    `notificationType` VARCHAR(191) NOT NULL,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `customMessage` TEXT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `idx_discord_config_server`(`serverId`),
    INDEX `idx_discord_config_type`(`notificationType`),
    INDEX `idx_discord_config_active`(`isActive`),
    UNIQUE INDEX `unique_server_channel_type`(`serverId`, `channelId`, `notificationType`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `push_2fa_device` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `userId` INTEGER NOT NULL,
    `deviceToken` VARCHAR(500) NOT NULL,
    `deviceName` VARCHAR(100) NULL,
    `deviceType` ENUM('IOS', 'ANDROID', 'WEB') NOT NULL,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `lastUsed` DATETIME(3) NULL,

    INDEX `idx_user_devices`(`userId`),
    INDEX `idx_device_token`(`deviceToken`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `push_2fa_challenge` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `userId` INTEGER NOT NULL,
    `challengeCode` VARCHAR(6) NOT NULL,
    `sessionToken` VARCHAR(100) NOT NULL,
    `deviceId` INTEGER NOT NULL,
    `status` ENUM('PENDING', 'APPROVED', 'DENIED', 'EXPIRED') NOT NULL DEFAULT 'PENDING',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `expiresAt` DATETIME(3) NOT NULL,
    `respondedAt` DATETIME(3) NULL,
    `ipAddress` VARCHAR(45) NULL,
    `userAgent` TEXT NULL,

    INDEX `idx_challenge_code`(`challengeCode`),
    INDEX `idx_session_token`(`sessionToken`),
    INDEX `idx_user_challenges`(`userId`),
    INDEX `idx_status_expires`(`status`, `expiresAt`),
    INDEX `push_2fa_challenge_deviceId_fkey`(`deviceId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `discordapplicationtype` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(100) NOT NULL,
    `discordBotId` INTEGER NOT NULL,
    `createdBy` INTEGER NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `visibleToRoles` TEXT NOT NULL DEFAULT 'VERIAGENT,MODERATOR,DEVELOPERS,ADMIN,SUPER_ADMIN',

    INDEX `discordapplicationtype_discordBotId_idx`(`discordBotId`),
    INDEX `discordapplicationtype_createdBy_idx`(`createdBy`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `discordapplication` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `typeId` INTEGER NOT NULL,
    `applicantId` VARCHAR(100) NOT NULL,
    `applicantName` VARCHAR(100) NOT NULL,
    `data` LONGTEXT NOT NULL,
    `status` ENUM('PENDING', 'APPROVED', 'REJECTED') NOT NULL DEFAULT 'PENDING',
    `submittedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `reviewedAt` DATETIME(3) NULL,
    `reviewedBy` INTEGER NULL,

    INDEX `discordapplication_typeId_idx`(`typeId`),
    INDEX `discordapplication_status_idx`(`status`),
    INDEX `discordapplication_applicantId_idx`(`applicantId`),
    INDEX `discordapplication_submittedAt_idx`(`submittedAt`),
    INDEX `discordapplication_reviewedBy_idx`(`reviewedBy`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `push_2fa_device` ADD CONSTRAINT `push_2fa_device_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `push_2fa_challenge` ADD CONSTRAINT `push_2fa_challenge_deviceId_fkey` FOREIGN KEY (`deviceId`) REFERENCES `push_2fa_device`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `push_2fa_challenge` ADD CONSTRAINT `push_2fa_challenge_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE `discordapplicationtype` ADD CONSTRAINT `discordapplicationtype_discordBotId_fkey` FOREIGN KEY (`discordBotId`) REFERENCES `discordbot`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `discordapplication` ADD CONSTRAINT `discordapplication_reviewedBy_fkey` FOREIGN KEY (`reviewedBy`) REFERENCES `user`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `discordapplication` ADD CONSTRAINT `discordapplication_typeId_fkey` FOREIGN KEY (`typeId`) REFERENCES `discordapplicationtype`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
