package org.xyzh.study.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.study.course.CourseService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.study.TbCourse;
import org.xyzh.common.dto.study.TbCourseChapter;
import org.xyzh.common.dto.study.TbCourseNode;
import org.xyzh.common.dto.study.TbLearningRecord;
import org.xyzh.common.vo.ChapterVO;
import org.xyzh.common.vo.CourseVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


/**
 * @description 课程控制器
 * @filename CourseController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/study/courses")
public class CourseController {
    private static final Logger logger = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    private CourseService courseService;

    /**
     * 获取课程列表
     */
    @GetMapping("/list")
    public ResultDomain<TbCourse> getCourseList(TbCourse filter) {
        return courseService.getCourseList(filter);
    }

    /**
     * 分页获取课程列表
     */
    @PostMapping("/page")
    public ResultDomain<TbCourse> getCoursePage(@RequestBody PageRequest<TbCourse> pageRequest) {
        return courseService.getCoursePage(pageRequest);
    }

    /**
     * 根据ID获取课程详情
     */
    @GetMapping("/{courseID}")
    public ResultDomain<CourseVO> getCourseById(@PathVariable("courseID") String courseID) {
        return courseService.getCourseById(courseID);
    }

    /**
     * 创建课程
     */
    @PostMapping("/course")
    public ResultDomain<CourseVO> createCourse(@RequestBody CourseVO courseVO) {
        return courseService.createCourse(courseVO);
    }

    /**
     * 更新课程基本信息
     */
    @PutMapping("/course")
    public ResultDomain<CourseVO> updateCourse(@RequestBody CourseVO courseVO) {
        return courseService.updateCourse(courseVO);
    }

    /**
     * 删除课程
     */
    @DeleteMapping("/course")
    public ResultDomain<Boolean> deleteCourse(@PathVariable("courseID") String courseID) {
        return courseService.deleteCourse(courseID);
    }

    /**
     * @description 创建课程章节
     * @param chapter 课程章节
     * @return ResultDomain<TbCourseChapter> 创建结果
     * @author yslg
     * @since 2025-10-22
     */
    @PostMapping("/course/chapter")
    public ResultDomain<TbCourseChapter> createChapter(@RequestBody TbCourseChapter chapter) {
        return courseService.createChapter(chapter);
    }

    /**
     * 更新课程章节
     */
    @PutMapping("/course/chapter")
    public ResultDomain<TbCourseChapter> updateChapter(@RequestBody TbCourseChapter chapter) {
        return courseService.updateChapter(chapter);
    }

    /**
     * 删除课程章节
     */
    @DeleteMapping("/course/chapter/{chapterID}")
    public ResultDomain<Boolean> deleteChapter(@PathVariable("chapterID") String chapterID) {
        return courseService.deleteChapter(chapterID);
    }

    /**
     * @description 创建课程章节节点
     * @param node 课程章节节点
     * @return ResultDomain<TbCourseNode> 创建结果
     * @author    yslg
     * @since 2025-10-22
     */
    @PostMapping("/course/chapter/node")
    public ResultDomain<TbCourseNode> createChapterNode(@RequestBody TbCourseNode node) {
        return courseService.createChapterNode(node);
    }

    /**
     * 更新课程章节节点
     */
    @PutMapping("/course/chapter/node")
    public ResultDomain<TbCourseNode> updateChapterNode(@RequestBody TbCourseNode node) {
        return courseService.updateChapterNode(node);
    }

    /**
     * 删除课程章节节点
     */
    @DeleteMapping("/course/chapter/node/{nodeID}")
    public ResultDomain<Boolean> deleteChapterNode(@PathVariable("nodeID") String nodeID) {
        return courseService.deleteChapterNode(nodeID);
    }

    /**
     * 更新课程状态
     */
    @PutMapping("/course/status")
    public ResultDomain<TbCourse> updateCourseStatus(@RequestBody TbCourse course) {
        return courseService.updateCourseStatus(course);
    }

    /**
     * 增加课程浏览次数
     */
    @PostMapping("/{courseID}/view")
    public ResultDomain<Boolean> incrementViewCount(@PathVariable("courseID") String courseID) {
        return courseService.incrementViewCount(courseID);
    }

    /**
     * 增加课程学习人数
     */
    @PostMapping("/{courseID}/learn")
    public ResultDomain<TbCourse> incrementLearnCount(@PathVariable("courseID") String courseID) {
        return courseService.incrementLearnCount(courseID);
    }

}
