-- 创建数据库（如果不存在）
CREATE DATABASE IF NOT EXISTS kaopeilian CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE kaopeilian;

-- 课程表
CREATE TABLE IF NOT EXISTS courses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(200) NOT NULL COMMENT '课程名称',
    description TEXT COMMENT '课程描述',
    category ENUM('technology', 'management', 'business', 'general') DEFAULT 'general' COMMENT '课程分类',
    status ENUM('draft', 'published', 'archived') DEFAULT 'draft' COMMENT '课程状态',
    cover_image VARCHAR(500) COMMENT '封面图片URL',
    duration_hours FLOAT COMMENT '课程时长(小时)',
    difficulty_level INT COMMENT '难度等级(1-5)',
    tags JSON COMMENT '标签列表',
    published_at DATETIME COMMENT '发布时间',
    publisher_id INT COMMENT '发布人ID',
    sort_order INT DEFAULT 0 COMMENT '排序顺序',
    is_featured BOOLEAN DEFAULT FALSE COMMENT '是否推荐',
    is_deleted BOOLEAN DEFAULT FALSE COMMENT '是否删除',
    deleted_at DATETIME COMMENT '删除时间',
    created_by INT COMMENT '创建人ID',
    updated_by INT COMMENT '更新人ID',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_status (status),
    INDEX idx_category (category),
    INDEX idx_is_featured (is_featured),
    INDEX idx_is_deleted (is_deleted),
    INDEX idx_sort_order (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='课程表';

-- 课程资料表
CREATE TABLE IF NOT EXISTS course_materials (
    id INT AUTO_INCREMENT PRIMARY KEY,
    course_id INT NOT NULL COMMENT '课程ID',
    name VARCHAR(200) NOT NULL COMMENT '资料名称',
    description TEXT COMMENT '资料描述',
    file_url VARCHAR(500) NOT NULL COMMENT '文件URL',
    file_type VARCHAR(50) NOT NULL COMMENT '文件类型',
    file_size INT NOT NULL COMMENT '文件大小(字节)',
    sort_order INT DEFAULT 0 COMMENT '排序顺序',
    is_deleted BOOLEAN DEFAULT FALSE COMMENT '是否删除',
    deleted_at DATETIME COMMENT '删除时间',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE,
    INDEX idx_course_id (course_id),
    INDEX idx_is_deleted (is_deleted)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='课程资料表';

-- 知识点表
CREATE TABLE IF NOT EXISTS knowledge_points (
    id INT AUTO_INCREMENT PRIMARY KEY,
    course_id INT NOT NULL COMMENT '课程ID',
    name VARCHAR(200) NOT NULL COMMENT '知识点名称',
    description TEXT COMMENT '知识点描述',
    parent_id INT COMMENT '父知识点ID',
    level INT DEFAULT 1 COMMENT '层级深度',
    path VARCHAR(500) COMMENT '路径(如: 1.2.3)',
    sort_order INT DEFAULT 0 COMMENT '排序顺序',
    weight FLOAT DEFAULT 1.0 COMMENT '权重',
    is_required BOOLEAN DEFAULT TRUE COMMENT '是否必修',
    estimated_hours FLOAT COMMENT '预计学习时间(小时)',
    is_deleted BOOLEAN DEFAULT FALSE COMMENT '是否删除',
    deleted_at DATETIME COMMENT '删除时间',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE,
    FOREIGN KEY (parent_id) REFERENCES knowledge_points(id) ON DELETE CASCADE,
    INDEX idx_course_id (course_id),
    INDEX idx_parent_id (parent_id),
    INDEX idx_is_deleted (is_deleted),
    INDEX idx_sort_order (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='知识点表';

-- 成长路径表
CREATE TABLE IF NOT EXISTS growth_paths (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(200) NOT NULL COMMENT '路径名称',
    description TEXT COMMENT '路径描述',
    target_role VARCHAR(100) COMMENT '目标角色',
    courses JSON COMMENT '课程列表[{course_id, order, is_required}]',
    estimated_duration_days INT COMMENT '预计完成天数',
    is_active BOOLEAN DEFAULT TRUE COMMENT '是否启用',
    sort_order INT DEFAULT 0 COMMENT '排序顺序',
    is_deleted BOOLEAN DEFAULT FALSE COMMENT '是否删除',
    deleted_at DATETIME COMMENT '删除时间',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_is_active (is_active),
    INDEX idx_is_deleted (is_deleted),
    INDEX idx_sort_order (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='成长路径表';

-- 插入测试数据
INSERT INTO courses (name, description, category, status, difficulty_level, tags, is_featured) VALUES
('Python编程基础', 'Python语言入门课程，适合零基础学员', 'technology', 'published', 2, '["Python", "编程基础", "入门"]', TRUE),
('项目管理实战', '学习现代项目管理方法和工具', 'management', 'published', 3, '["项目管理", "敏捷", "Scrum"]', FALSE),
('商务沟通技巧', '提升职场沟通能力', 'business', 'draft', 2, '["沟通", "软技能", "职场"]', FALSE);

-- 为第一个课程添加资料
INSERT INTO course_materials (course_id, name, description, file_url, file_type, file_size) VALUES
(1, 'Python基础教程.pdf', 'Python编程基础教程文档', '/uploads/python-basics.pdf', 'pdf', 2048000),
(1, '课程视频1', '第一章节视频教程', '/uploads/video1.mp4', 'mp4', 104857600);

-- 为第一个课程添加知识点
INSERT INTO knowledge_points (course_id, name, description, parent_id, level, weight, estimated_hours) VALUES
(1, 'Python环境搭建', '学习如何安装和配置Python开发环境', NULL, 1, 1.0, 2),
(1, 'Python基础语法', '学习Python的基本语法规则', NULL, 1, 2.0, 8),
(1, '变量和数据类型', '了解Python中的变量和基本数据类型', 2, 2, 1.5, 3),
(1, '控制流程', '学习条件语句和循环结构', 2, 2, 1.5, 4);
