-- CreateTable
CREATE TABLE `sys_api_permission` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `menu_id` INTEGER NOT NULL,
    `path` VARCHAR(191) NOT NULL,
    `method` VARCHAR(191) NOT NULL,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `sys_api_permission_menu_id_path_method_key`(`menu_id`, `path`, `method`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `CacheSyncAction` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `entityType` VARCHAR(50) NOT NULL,
    `entityId` BIGINT NULL,
    `operation` VARCHAR(50) NOT NULL,
    `status` VARCHAR(20) NOT NULL,
    `attempts` INTEGER NOT NULL DEFAULT 0,
    `nextAttemptAt` BIGINT NULL,
    `lastError` VARCHAR(500) NULL,
    `payload` JSON NULL,
    `createdAt` BIGINT NOT NULL,
    `updatedAt` BIGINT NOT NULL,

    INDEX `CacheSyncAction_status_nextAttemptAt_idx`(`status`, `nextAttemptAt`),
    INDEX `CacheSyncAction_entityType_entityId_idx`(`entityType`, `entityId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `image_config` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `channel_id` INTEGER NULL,
    `provider_decode_base` VARCHAR(191) NULL,
    `local_base_url` VARCHAR(191) NULL,
    `s3_base_url` VARCHAR(191) NULL,
    `preferred_source` ENUM('PROVIDER', 'LOCAL_ONLY', 'S3_ONLY', 'S3_AND_LOCAL') NOT NULL DEFAULT 'PROVIDER',
    `status` INTEGER NOT NULL DEFAULT 1,
    `create_at` BIGINT NOT NULL DEFAULT 0,
    `update_at` BIGINT NOT NULL DEFAULT 0,

    INDEX `idx_image_config_channel_id`(`channel_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sys_login_log` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `type` ENUM('LOGIN', 'LOGOUT') NOT NULL,
    `status` ENUM('SUCCESS', 'USERNAME_OR_PASSWORD_ERROR', 'USER_DISABLED', 'IP_LIMITED', 'TWO_FA_CODE_ERROR', 'TWO_FA_NOT_ENABLED', 'TWO_FA_REQUIRED') NOT NULL,
    `username` VARCHAR(191) NOT NULL,
    `ip_address` VARCHAR(191) NOT NULL,
    `user_agent` VARCHAR(191) NOT NULL,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `ProviderVideoSync` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `providerCode` VARCHAR(191) NOT NULL,
    `providerVideoId` VARCHAR(191) NOT NULL,
    `videoMediaId` VARCHAR(191) NOT NULL,
    `lastProviderUpdatedAt` BIGINT NOT NULL,
    `lastSyncedAt` BIGINT NOT NULL,
    `syncStatus` INTEGER NOT NULL DEFAULT 0,
    `lastError` VARCHAR(500) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sys_menu` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `parent_id` INTEGER NULL,
    `title` VARCHAR(191) NOT NULL,
    `status` BOOLEAN NOT NULL DEFAULT true,
    `type` ENUM('DIRECTORY', 'MENU', 'SUBMENU', 'BUTTON') NOT NULL,
    `order` INTEGER NOT NULL,
    `frontend_auth` VARCHAR(191) NULL,
    `path` VARCHAR(191) NULL,
    `name` VARCHAR(191) NULL,
    `icon` VARCHAR(191) NULL,
    `redirect` VARCHAR(191) NULL,
    `component_key` VARCHAR(191) NULL,
    `meta` JSON NULL,
    `canView` INTEGER NOT NULL DEFAULT 0,
    `canCreate` INTEGER NOT NULL DEFAULT 0,
    `canUpdate` INTEGER NOT NULL DEFAULT 0,
    `canDelete` INTEGER NOT NULL DEFAULT 0,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE `sys_operation_log` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(191) NULL,
    `menu_id` INTEGER NULL,
    `description` VARCHAR(191) NOT NULL,
    `type` ENUM('CREATE', 'READ', 'UPDATE', 'DELETE') NOT NULL,
    `status` BOOLEAN NOT NULL,
    `method` VARCHAR(191) NOT NULL,
    `path` VARCHAR(191) NOT NULL,
    `body` JSON NULL,
    `response` JSON NULL,
    `ip_address` VARCHAR(191) NOT NULL,
    `call_method` VARCHAR(191) NOT NULL,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sys_quota_log` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(191) NOT NULL,
    `op_username` VARCHAR(191) NOT NULL,
    `amount` BIGINT NOT NULL,
    `is_inc` BOOLEAN NOT NULL,
    `quota` BIGINT NOT NULL,
    `remark` VARCHAR(191) NULL,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sys_role_api_permission` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `role_id` INTEGER NOT NULL,
    `api_permission_id` INTEGER NOT NULL,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `sys_role_api_permission_role_id_api_permission_id_key`(`role_id`, `api_permission_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sys_role_menu` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `role_id` INTEGER NOT NULL,
    `menu_id` INTEGER NOT NULL,
    `canView` INTEGER NOT NULL DEFAULT 0,
    `canCreate` INTEGER NOT NULL DEFAULT 0,
    `canUpdate` INTEGER NOT NULL DEFAULT 0,
    `canDelete` INTEGER NOT NULL DEFAULT 0,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `sys_role_menu_role_id_menu_id_key`(`role_id`, `menu_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sys_role` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `status` BOOLEAN NOT NULL DEFAULT true,
    `remark` VARCHAR(191) NULL,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE `sys_user_role` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NOT NULL,
    `role_id` INTEGER NOT NULL,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `sys_user_role_user_id_role_id_key`(`user_id`, `role_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sys_user` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(191) NOT NULL,
    `password` VARCHAR(191) NOT NULL,
    `status` INTEGER NOT NULL DEFAULT 1,
    `nick` VARCHAR(100) NULL,
    `photo` VARCHAR(1024) NULL,
    `remark` VARCHAR(256) NULL,
    `twoFA` VARCHAR(256) NULL,
    `twoFALastUsedStep` INTEGER NULL,
    `twoFARecoveryCodes` JSON NULL,
    `create_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `allowIps` JSON NULL,
    `jwtToken` VARCHAR(1024) NULL,
    `oAuthJwtToken` VARCHAR(1024) NULL,
    `lastLoginTime` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

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

-- AddForeignKey
ALTER TABLE `sys_api_permission` ADD CONSTRAINT `sys_api_permission_menu_id_fkey` FOREIGN KEY (`menu_id`) REFERENCES `sys_menu`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sys_menu` ADD CONSTRAINT `sys_menu_parent_id_fkey` FOREIGN KEY (`parent_id`) REFERENCES `sys_menu`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sys_role_api_permission` ADD CONSTRAINT `sys_role_api_permission_role_id_fkey` FOREIGN KEY (`role_id`) REFERENCES `sys_role`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sys_role_api_permission` ADD CONSTRAINT `sys_role_api_permission_api_permission_id_fkey` FOREIGN KEY (`api_permission_id`) REFERENCES `sys_api_permission`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sys_role_menu` ADD CONSTRAINT `sys_role_menu_role_id_fkey` FOREIGN KEY (`role_id`) REFERENCES `sys_role`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sys_role_menu` ADD CONSTRAINT `sys_role_menu_menu_id_fkey` FOREIGN KEY (`menu_id`) REFERENCES `sys_menu`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sys_user_role` ADD CONSTRAINT `sys_user_role_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `sys_user`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sys_user_role` ADD CONSTRAINT `sys_user_role_role_id_fkey` FOREIGN KEY (`role_id`) REFERENCES `sys_role`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
