-- 表1：用户表
CREATE TABLE IF NOT EXISTS `users` (
  `id` INT NOT NULL COMMENT 'NewAPI 用户 ID，作为本站用户主键',
  `username` VARCHAR(191) NOT NULL DEFAULT '' COMMENT '用户名快照',
  `display_name` VARCHAR(191) NOT NULL DEFAULT '' COMMENT '展示名称快照',
  `group` VARCHAR(191) NOT NULL DEFAULT '' COMMENT '用户分组快照',
  `role` INT NOT NULL DEFAULT 0 COMMENT '用户角色快照',
  `status` INT NOT NULL DEFAULT 0 COMMENT '用户状态快照',
  `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
  `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';

-- 表2：生图记录表
CREATE TABLE IF NOT EXISTS `image_generations` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '生图记录 ID',
  `user_id` INT NOT NULL COMMENT 'NewAPI 用户 ID',
  `prompt` TEXT NOT NULL COMMENT '用户输入的生图提示词',
  `status` ENUM('QUEUED', 'RUNNING', 'SUCCEEDED', 'FAILED') NOT NULL DEFAULT 'RUNNING' COMMENT '生图状态',
  `model` VARCHAR(64) NOT NULL DEFAULT 'gpt-image-2' COMMENT '生图模型',
  `started_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '生图开始时间',
  `ended_at` DATETIME(3) NULL COMMENT '生图结束时间',
  `duration_ms` INT NULL COMMENT '生图耗时，单位毫秒',
  `image_url` VARCHAR(2048) NULL COMMENT 'NewAPI 上游返回的图片 URL',
  `hosted_image_url` VARCHAR(2048) NULL COMMENT 'Lsky 图床归档后的图片 URL',
  `image_mime_type` VARCHAR(191) NULL COMMENT '图片 MIME 类型',
  `revised_prompt` TEXT NULL COMMENT '上游返回的修订提示词',
  `upstream_response` JSON NULL COMMENT '完整上游生图接口响应',
  `hosted_response` JSON NULL COMMENT '完整 Lsky 图床上传接口响应',
  `error_message` TEXT NULL COMMENT '失败原因或图床归档失败提示',
  `archive_status` ENUM('NOT_REQUIRED', 'PENDING', 'RUNNING', 'SUCCEEDED', 'FAILED') NOT NULL DEFAULT 'NOT_REQUIRED' COMMENT '图床归档任务状态',
  `archive_attempts` INT NOT NULL DEFAULT 0 COMMENT '图床归档已尝试次数',
  `archive_next_run_at` DATETIME(3) NULL COMMENT '图床归档下次可执行时间',
  `archive_locked_at` DATETIME(3) NULL COMMENT '图床归档任务被 worker 锁定的时间',
  `archive_locked_by` VARCHAR(191) NULL COMMENT '图床归档任务 worker 标识',
  `archive_last_error` TEXT NULL COMMENT '图床归档最近一次失败摘要',
  `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
  `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间',
  `deleted_at` DATETIME(3) NULL COMMENT '软删除时间',
  PRIMARY KEY (`id`),
  INDEX `image_generations_user_id_created_at_idx` (`user_id`, `created_at`),
  INDEX `image_generations_status_idx` (`status`),
  INDEX `image_generations_archive_status_archive_next_run_at_idx` (`archive_status`, `archive_next_run_at`),
  INDEX `image_generations_archive_locked_at_idx` (`archive_locked_at`),
  INDEX `image_generations_deleted_at_idx` (`deleted_at`),
  CONSTRAINT `image_generations_user_id_fkey`
    FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
    ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='生图记录表';

-- 表3：生图统计表
CREATE TABLE IF NOT EXISTS `generation_stats` (
  `id` VARCHAR(32) NOT NULL COMMENT '统计行 ID，固定为 global',
  `total_requests` INT NOT NULL DEFAULT 0 COMMENT '总生图请求数，包含成功和失败',
  `success_requests` INT NOT NULL DEFAULT 0 COMMENT '成功生图请求数',
  `failed_requests` INT NOT NULL DEFAULT 0 COMMENT '失败生图请求数',
  `queued_requests` INT NOT NULL DEFAULT 0 COMMENT '排队中请求数，预留异步任务使用',
  `running_requests` INT NOT NULL DEFAULT 0 COMMENT '进行中请求数',
  `total_images` INT NOT NULL DEFAULT 0 COMMENT '成功生成图片总数',
  `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
  `updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='生图统计表';
