/*
  Warnings:

  - The values [PUSH,BOTH] on the enum `user_preferredTwoFactorMethod` will be removed. If these variants are still used in the database, this will fail.
  - You are about to alter the column `credentialId` on the `webauthn_credential` table. The data in that column could be lost. The data in that column will be cast from `VarChar(255)` to `VarChar(191)`.
  - You are about to alter the column `transports` on the `webauthn_credential` table. The data in that column could be lost. The data in that column will be cast from `VarChar(255)` to `VarChar(191)`.

*/
-- DropForeignKey
ALTER TABLE `webauthn_credential` DROP FOREIGN KEY `fk_webauthn_user`;

-- AlterTable
ALTER TABLE `discordbot` ADD COLUMN `announcementChannelId` VARCHAR(191) NULL,
    ADD COLUMN `announcementColor` VARCHAR(191) NULL DEFAULT '#f97316',
    ADD COLUMN `changelogChannelId` VARCHAR(191) NULL,
    ADD COLUMN `changelogColor` VARCHAR(191) NULL DEFAULT '#22c55e',
    ADD COLUMN `changelogRoleId` VARCHAR(191) NULL,
    ADD COLUMN `teamName` VARCHAR(191) NULL DEFAULT 'Team Nightbox';

-- AlterTable
ALTER TABLE `report` ADD COLUMN `violations` TEXT NULL;

-- AlterTable
ALTER TABLE `user` MODIFY `preferredTwoFactorMethod` ENUM('TOTP') NOT NULL DEFAULT 'TOTP';

-- AlterTable
ALTER TABLE `webauthn_credential` MODIFY `credentialId` VARCHAR(191) NOT NULL,
    MODIFY `transports` VARCHAR(191) NULL;

-- CreateTable
CREATE TABLE `document` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(191) NOT NULL,
    `content` LONGTEXT NULL,
    `excerpt` TEXT NULL,
    `category` VARCHAR(191) NULL,
    `tags` TEXT NULL,
    `fileUrl` VARCHAR(191) NULL,
    `fileName` VARCHAR(191) NULL,
    `fileType` VARCHAR(191) NULL,
    `fileSize` INTEGER NULL,
    `status` ENUM('DRAFT', 'PUBLISHED', 'ARCHIVED') NOT NULL DEFAULT 'DRAFT',
    `visibility` ENUM('PUBLIC', 'INTERNAL', 'RESTRICTED') NOT NULL DEFAULT 'INTERNAL',
    `version` INTEGER NOT NULL DEFAULT 1,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `publishedAt` DATETIME(3) NULL,
    `authorId` INTEGER NOT NULL,
    `visibleToRoles` TEXT NOT NULL DEFAULT 'VERIAGENT,MODERATOR,DEVELOPERS,ADMIN,SUPER_ADMIN',

    INDEX `idx_document_author`(`authorId`),
    INDEX `idx_document_category`(`category`),
    INDEX `idx_document_status`(`status`),
    INDEX `idx_document_visibility`(`visibility`),
    INDEX `idx_document_created`(`createdAt`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `document_version` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `documentId` INTEGER NOT NULL,
    `version` INTEGER NOT NULL,
    `title` VARCHAR(191) NOT NULL,
    `content` LONGTEXT NULL,
    `changes` TEXT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `createdBy` INTEGER NOT NULL,

    INDEX `idx_document_version_doc`(`documentId`),
    INDEX `idx_document_version_user`(`createdBy`),
    UNIQUE INDEX `document_version_documentId_version_key`(`documentId`, `version`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `txadmin_user_config` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `userId` INTEGER NOT NULL,
    `username` VARCHAR(191) NOT NULL,
    `password` TEXT NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `txadmin_user_config_userId_key`(`userId`),
    INDEX `idx_txadmin_user_config_user`(`userId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `txadmin_server_config` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `serverUrl` VARCHAR(191) NOT NULL,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE `ftp_server` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NOT NULL,
    `name` VARCHAR(255) NOT NULL,
    `host` VARCHAR(255) NOT NULL,
    `port` INTEGER NOT NULL DEFAULT 21,
    `username` VARCHAR(255) NOT NULL,
    `password` TEXT NULL,
    `protocol` ENUM('FTP', 'FTPS', 'SFTP') NOT NULL DEFAULT 'FTP',
    `encryption` ENUM('None', 'SSL', 'TLS') NOT NULL DEFAULT 'None',
    `passive_mode` BOOLEAN NOT NULL DEFAULT true,
    `ssl` BOOLEAN NOT NULL DEFAULT false,
    `remote_path` VARCHAR(500) NOT NULL DEFAULT '/',
    `remember_password` BOOLEAN NOT NULL DEFAULT true,
    `server_type` ENUM('development', 'production') NOT NULL DEFAULT 'development',
    `last_connected` DATETIME(3) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `ftp_server_user_id_fkey`(`user_id`),
    INDEX `idx_ftp_server_user_type`(`user_id`, `server_type`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

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

-- AddForeignKey
ALTER TABLE `document` ADD CONSTRAINT `document_authorId_fkey` FOREIGN KEY (`authorId`) REFERENCES `user`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE `document_version` ADD CONSTRAINT `document_version_documentId_fkey` FOREIGN KEY (`documentId`) REFERENCES `document`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE `ftp_server` ADD CONSTRAINT `ftp_server_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- RedefineIndex
CREATE UNIQUE INDEX `webauthn_credential_credentialId_key` ON `webauthn_credential`(`credentialId`);
DROP INDEX `credentialId_unique` ON `webauthn_credential`;
