-- CreateTable
CREATE TABLE `forum_thread_files` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `thread_id` INTEGER NOT NULL,
    `file_id` INTEGER NOT NULL,
    `upload_order` INTEGER NOT NULL DEFAULT 0,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `forum_thread_files_thread_id_idx`(`thread_id`),
    INDEX `forum_thread_files_file_id_idx`(`file_id`),
    UNIQUE INDEX `forum_thread_files_thread_id_file_id_key`(`thread_id`, `file_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `forum_reply_files` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `reply_id` INTEGER NOT NULL,
    `file_id` INTEGER NOT NULL,
    `upload_order` INTEGER NOT NULL DEFAULT 0,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `forum_reply_files_reply_id_idx`(`reply_id`),
    INDEX `forum_reply_files_file_id_idx`(`file_id`),
    UNIQUE INDEX `forum_reply_files_reply_id_file_id_key`(`reply_id`, `file_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `forum_thread_files` ADD CONSTRAINT `forum_thread_files_thread_id_fkey` FOREIGN KEY (`thread_id`) REFERENCES `forum_threads`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `forum_thread_files` ADD CONSTRAINT `forum_thread_files_file_id_fkey` FOREIGN KEY (`file_id`) REFERENCES `files`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `forum_reply_files` ADD CONSTRAINT `forum_reply_files_reply_id_fkey` FOREIGN KEY (`reply_id`) REFERENCES `forum_replies`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `forum_reply_files` ADD CONSTRAINT `forum_reply_files_file_id_fkey` FOREIGN KEY (`file_id`) REFERENCES `files`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
