-- 星域故事汇数据库初始化脚本
-- 创建数据库
CREATE DATABASE IF NOT EXISTS stardom_story DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE stardom_story;

-- 故事主表
CREATE TABLE IF NOT EXISTS stories (
  id INT PRIMARY KEY 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 BOOLEAN DEFAULT FALSE COMMENT '是否精选',
  status TINYINT DEFAULT 1 COMMENT '状态：0下架 1上架',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_category (category),
  INDEX idx_featured (is_featured),
  INDEX idx_play_count (play_count)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='故事主表';

-- 故事节点表
CREATE TABLE IF NOT EXISTS story_nodes (
  id INT PRIMARY KEY 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 BOOLEAN DEFAULT FALSE 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,
  INDEX idx_story_id (story_id),
  INDEX idx_node_key (story_id, node_key),
  FOREIGN KEY (story_id) REFERENCES stories(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='故事节点表';

-- 故事选项表
CREATE TABLE IF NOT EXISTS story_choices (
  id INT PRIMARY KEY 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 BOOLEAN DEFAULT FALSE COMMENT '是否锁定（需广告解锁）',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_node_id (node_id),
  INDEX idx_story_id (story_id),
  FOREIGN KEY (node_id) REFERENCES story_nodes(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='故事选项表';

-- 用户表
CREATE TABLE IF NOT EXISTS users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  openid VARCHAR(100) UNIQUE 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,
  INDEX idx_openid (openid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 用户进度表
CREATE TABLE IF NOT EXISTS user_progress (
  id INT PRIMARY KEY 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 BOOLEAN DEFAULT FALSE COMMENT '是否完成',
  ending_reached VARCHAR(100) DEFAULT '' COMMENT '达成的结局',
  is_liked BOOLEAN DEFAULT FALSE COMMENT '是否点赞',
  is_collected BOOLEAN DEFAULT FALSE COMMENT '是否收藏',
  play_count INT DEFAULT 1 COMMENT '游玩次数',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_user_story (user_id, story_id),
  INDEX idx_user_id (user_id),
  INDEX idx_story_id (story_id),
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  FOREIGN KEY (story_id) REFERENCES stories(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户进度表';

-- 用户结局收集表
CREATE TABLE IF NOT EXISTS user_endings (
  id INT PRIMARY KEY 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,
  UNIQUE KEY uk_user_ending (user_id, story_id, ending_name),
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  FOREIGN KEY (story_id) REFERENCES stories(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户结局收集表';
