-- Content 服务数据库初始化脚本
-- 表：content_textbooks / content_chapters / content_knowledge_points / content_questions
-- 表结构与 services/content/src 下的 Drizzle schema 对齐

CREATE TABLE IF NOT EXISTS content_textbooks (
  id CHAR(36) NOT NULL PRIMARY KEY,
  title VARCHAR(200) NOT NULL,
  subject_id CHAR(36) NOT NULL,
  grade_id CHAR(36) NOT NULL,
  version VARCHAR(50) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_textbooks_subject_id (subject_id),
  INDEX idx_textbooks_grade_id (grade_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS content_chapters (
  id CHAR(36) NOT NULL PRIMARY KEY,
  textbook_id CHAR(36) NOT NULL,
  title VARCHAR(200) NOT NULL,
  order_num INT NOT NULL,
  parent_id CHAR(36),
  INDEX idx_chapters_textbook_id (textbook_id),
  INDEX idx_chapters_parent_id (parent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS content_knowledge_points (
  id CHAR(36) NOT NULL PRIMARY KEY,
  chapter_id CHAR(36) NOT NULL,
  title VARCHAR(200) NOT NULL,
  description TEXT,
  INDEX idx_knowledge_points_chapter_id (chapter_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS content_questions (
  id CHAR(36) NOT NULL PRIMARY KEY,
  knowledge_point_id CHAR(36) NOT NULL,
  type VARCHAR(50) NOT NULL,
  content TEXT NOT NULL,
  answer TEXT,
  explanation TEXT,
  difficulty INT DEFAULT 3,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_questions_knowledge_point_id (knowledge_point_id),
  INDEX idx_questions_type (type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
