-- ============================================
-- 星域故事汇数据库结构
-- 基于实际数据库导出，共7张表
-- ============================================

CREATE DATABASE IF NOT EXISTS stardom_story DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE stardom_story;

-- ============================================
-- 1. 用户表
-- ============================================
CREATE TABLE IF NOT EXISTS `users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `openid` VARCHAR(100) NOT NULL COMMENT '微信openid',
  `nickname` VARCHAR(100) DEFAULT '' COMMENT '昵称',
  `avatar_url` VARCHAR(255) DEFAULT '' COMMENT '头像URL',
  `gender` TINYINT DEFAULT 0 COMMENT '性别：0未知 1男 2女',
  `total_play_count` INT DEFAULT 0 COMMENT '总游玩次数',
  `total_endings` INT DEFAULT 0 COMMENT '解锁结局数',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `openid` (`openid`),
  KEY `idx_openid` (`openid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- ============================================
-- 2. 故事主表
-- ============================================
CREATE TABLE IF NOT EXISTS `stories` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(100) NOT NULL COMMENT '故事标题',
  `cover_url` VARCHAR(255) DEFAULT '' COMMENT '封面图URL',
  `description` TEXT COMMENT '故事简介',
  `author_id` INT DEFAULT 0 COMMENT '作者ID，0表示官方',
  `category` VARCHAR(50) NOT NULL COMMENT '故事分类',
  `play_count` INT DEFAULT 0 COMMENT '游玩次数',
  `like_count` INT DEFAULT 0 COMMENT '点赞数',
  `is_featured` TINYINT(1) DEFAULT 0 COMMENT '是否精选',
  `status` TINYINT DEFAULT 1 COMMENT '状态：0下架 1上架',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_category` (`category`),
  KEY `idx_featured` (`is_featured`),
  KEY `idx_play_count` (`play_count`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='故事主表';

-- ============================================
-- 3. 故事节点表
-- ============================================
CREATE TABLE IF NOT EXISTS `story_nodes` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `story_id` INT NOT NULL COMMENT '故事ID',
  `node_key` VARCHAR(50) NOT NULL COMMENT '节点唯一标识',
  `content` TEXT NOT NULL COMMENT '节点内容文本',
  `speaker` VARCHAR(50) DEFAULT '' COMMENT '说话角色名',
  `background_image` VARCHAR(255) DEFAULT '' COMMENT '背景图URL',
  `character_image` VARCHAR(255) DEFAULT '' COMMENT '角色立绘URL',
  `bgm` VARCHAR(255) DEFAULT '' COMMENT '背景音乐',
  `is_ending` TINYINT(1) DEFAULT 0 COMMENT '是否为结局节点',
  `ending_name` VARCHAR(100) DEFAULT '' COMMENT '结局名称',
  `ending_score` INT DEFAULT 0 COMMENT '结局评分',
  `ending_type` VARCHAR(20) DEFAULT '' COMMENT '结局类型：good/bad/normal/hidden',
  `sort_order` INT DEFAULT 0 COMMENT '排序',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_story_id` (`story_id`),
  KEY `idx_node_key` (`story_id`, `node_key`),
  CONSTRAINT `story_nodes_ibfk_1` FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='故事节点表';

-- ============================================
-- 4. 故事选项表
-- ============================================
CREATE TABLE IF NOT EXISTS `story_choices` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `node_id` INT NOT NULL COMMENT '所属节点ID',
  `story_id` INT NOT NULL COMMENT '故事ID（冗余，便于查询）',
  `text` VARCHAR(200) NOT NULL COMMENT '选项文本',
  `next_node_key` VARCHAR(50) NOT NULL COMMENT '下一个节点key',
  `sort_order` INT DEFAULT 0 COMMENT '排序',
  `is_locked` TINYINT(1) DEFAULT 0 COMMENT '是否锁定（需广告解锁）',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_node_id` (`node_id`),
  KEY `idx_story_id` (`story_id`),
  CONSTRAINT `story_choices_ibfk_1` FOREIGN KEY (`node_id`) REFERENCES `story_nodes` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='故事选项表';

-- ============================================
-- 5. 用户进度表
-- ============================================
CREATE TABLE IF NOT EXISTS `user_progress` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL COMMENT '用户ID',
  `story_id` INT NOT NULL COMMENT '故事ID',
  `current_node_key` VARCHAR(50) DEFAULT 'start' COMMENT '当前节点',
  `is_completed` TINYINT(1) DEFAULT 0 COMMENT '是否完成',
  `ending_reached` VARCHAR(100) DEFAULT '' COMMENT '达成的结局',
  `is_liked` TINYINT(1) DEFAULT 0 COMMENT '是否点赞',
  `is_collected` TINYINT(1) DEFAULT 0 COMMENT '是否收藏',
  `play_count` INT DEFAULT 1 COMMENT '游玩次数',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_story` (`user_id`, `story_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_story_id` (`story_id`),
  CONSTRAINT `user_progress_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `user_progress_ibfk_2` FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户进度表';

-- ============================================
-- 6. 用户结局收集表
-- ============================================
CREATE TABLE IF NOT EXISTS `user_endings` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL,
  `story_id` INT NOT NULL,
  `ending_name` VARCHAR(100) NOT NULL,
  `ending_score` INT DEFAULT 0,
  `unlocked_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_ending` (`user_id`, `story_id`, `ending_name`),
  KEY `story_id` (`story_id`),
  CONSTRAINT `user_endings_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `user_endings_ibfk_2` FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户结局收集表';

-- ============================================
-- 7. AI改写草稿表
-- ============================================
CREATE TABLE IF NOT EXISTS `story_drafts` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL COMMENT '用户ID',
  `story_id` INT NOT NULL COMMENT '原故事ID',
  `title` VARCHAR(100) DEFAULT '' COMMENT '草稿标题',
  `path_history` JSON DEFAULT NULL COMMENT '用户之前的选择路径',
  `current_node_key` VARCHAR(50) DEFAULT '' COMMENT '改写起始节点',
  `current_content` TEXT COMMENT '当前节点内容',
  `user_prompt` VARCHAR(500) NOT NULL COMMENT '用户改写指令',
  `ai_nodes` JSON DEFAULT NULL COMMENT 'AI生成的新节点',
  `entry_node_key` VARCHAR(50) DEFAULT '' COMMENT '入口节点',
  `tokens_used` INT DEFAULT 0 COMMENT '消耗token数',
  `status` ENUM('pending', 'processing', 'completed', 'failed') DEFAULT 'pending' COMMENT '状态',
  `error_message` VARCHAR(500) DEFAULT '' COMMENT '失败原因',
  `is_read` TINYINT(1) DEFAULT 0 COMMENT '用户是否已查看',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `completed_at` TIMESTAMP NULL DEFAULT NULL COMMENT '完成时间',
  PRIMARY KEY (`id`),
  KEY `idx_user` (`user_id`),
  KEY `idx_story` (`story_id`),
  KEY `idx_status` (`status`),
  KEY `idx_user_unread` (`user_id`, `is_read`),
  CONSTRAINT `story_drafts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `story_drafts_ibfk_2` FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI改写草稿表';

-- ============================================
-- 8. 游玩记录表
-- ============================================
CREATE TABLE IF NOT EXISTS `play_records` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `user_id` INT NOT NULL COMMENT '用户ID',
  `story_id` INT NOT NULL COMMENT '故事ID',
  `ending_name` VARCHAR(100) NOT NULL COMMENT '结局名称',
  `ending_type` VARCHAR(20) DEFAULT '' COMMENT '结局类型',
  `path_history` JSON NOT NULL COMMENT '完整的选择路径',
  `play_duration` INT DEFAULT 0 COMMENT '游玩时长(秒)',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_story` (`user_id`, `story_id`),
  KEY `idx_user` (`user_id`),
  KEY `idx_story` (`story_id`),
  CONSTRAINT `play_records_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `play_records_ibfk_2` FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='游玩记录表';

-- ============================================
-- 9. 故事角色表
-- ============================================
CREATE TABLE IF NOT EXISTS `story_characters` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `story_id` INT NOT NULL COMMENT '所属故事ID',
  `name` VARCHAR(50) NOT NULL COMMENT '角色名称',
  `role_type` VARCHAR(20) DEFAULT 'supporting' COMMENT '角色类型: protagonist/antagonist/supporting',
  `gender` VARCHAR(10) DEFAULT '' COMMENT '性别: male/female/unknown',
  `age_range` VARCHAR(20) DEFAULT '' COMMENT '年龄段: child/teen/young/middle/old',
  `appearance` TEXT COMMENT '外貌描述（用于生成图片）',
  `personality` TEXT COMMENT '性格描述',
  `background` TEXT COMMENT '背景故事',
  `avatar_prompt` TEXT COMMENT 'AI绘图提示词',
  `avatar_url` VARCHAR(500) DEFAULT '' COMMENT '角色头像URL',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_story_id` (`story_id`),
  CONSTRAINT `story_characters_ibfk_1` FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='故事角色表';

-- ============================================
-- 10. 节点角色关联表
-- ============================================
CREATE TABLE IF NOT EXISTS `node_characters` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `story_id` INT NOT NULL COMMENT '故事ID',
  `node_key` VARCHAR(50) NOT NULL COMMENT '节点key',
  `character_id` INT NOT NULL COMMENT '角色ID',
  `is_speaker` TINYINT(1) DEFAULT 0 COMMENT '是否为该节点说话者',
  `emotion` VARCHAR(30) DEFAULT 'neutral' COMMENT '该节点情绪: happy/sad/angry/neutral等',
  PRIMARY KEY (`id`),
  KEY `idx_story_node` (`story_id`, `node_key`),
  KEY `idx_character` (`character_id`),
  CONSTRAINT `node_characters_ibfk_1` FOREIGN KEY (`character_id`) REFERENCES `story_characters` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='节点角色关联表';
