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.record.LearningRecordService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.study.TbLearningRecord;
import org.xyzh.common.vo.LearningStatisticsDetailVO;

import java.util.Map;


/**
 * @description 学习记录控制器
 * @filename LearningRecordController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/study/records")
public class LearningRecordController {
    private static final Logger logger = LoggerFactory.getLogger(LearningRecordController.class);

    @Autowired
    private LearningRecordService learningRecordService;

    /**
     * 获取学习记录
     */
    @PostMapping("/list")
    public ResultDomain<TbLearningRecord> getLearningRecordList(@RequestBody TbLearningRecord learningRecord) {
        return learningRecordService.getLearningRecord(learningRecord);
    }

    /**
     * 创建学习记录
     */
    @PostMapping("/record")
    public ResultDomain<TbLearningRecord> insertLearningRecord(@RequestBody TbLearningRecord record) {
        return learningRecordService.insertLearningRecord(record);
    }

    /**
     * 更新学习记录
     */
    @PutMapping("/record")
    public ResultDomain<TbLearningRecord> updateLearningRecord(@RequestBody TbLearningRecord record) {
        return learningRecordService.updateLearningRecord(record);
    }

    /**
     * 删除学习记录
     */
    @DeleteMapping("/record")
    public ResultDomain<Boolean> deleteLearningRecord(@RequestBody TbLearningRecord learningRecord) {
        return learningRecordService.deleteLearningRecord(learningRecord.getID());
    }

    @PutMapping("/complete")
    public ResultDomain<TbLearningRecord> markLearningComplete(@RequestBody TbLearningRecord learningRecord) {
        return learningRecordService.markLearningComplete(learningRecord);
    }

    /**
     * 查询用户课程的学习记录
     */
    @PostMapping("/course/records")
    public ResultDomain<TbLearningRecord> getCourseLearningRecord(@RequestBody TbLearningRecord learningRecord) {
        
        return learningRecordService.getCourseLearningRecord(learningRecord);
    }

    /**
     * 获取学习记录统计图表数据
     * @return 图表数据（本周课程和文章的总学习时长）
     */
    @GetMapping("/statistics/charts")
    public ResultDomain<Map<String, Object>> getStudyRecordsCharts() {
        return learningRecordService.getStudyRecordsCharts();
    }

    /**
     * 获取学习记录排行榜数据
     * @return 排行榜数据（学习时长排行榜、课程排行榜、文章排行榜、任务完成排行榜）
     */
    @GetMapping("/statistics/rankings")
    public ResultDomain<Map<String, Object>> getStudyRecordsRankings() {
        return learningRecordService.getStudyRecordsRankings();
    }

    /**
     * 获取用户学习记录统计
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 用户学习记录统计
     */
    @GetMapping("/user/record/range")
    public ResultDomain<Map<String, Object>> getUserRecordRange(@RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) {
        return learningRecordService.getUserRecordRange(startTime, endTime);
    }

        /**
     * 获取用户学习记录统计
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 用户学习记录统计
     */
    @PostMapping("/user/record/range")
    public ResultDomain<LearningStatisticsDetailVO> getUserRecordRangePage(@RequestBody PageRequest<TbLearningRecord> learningRecord) {
        return learningRecordService.getUserRecordRangePage(learningRecord);
    }


}
