package org.xyzh.study.controller;

import java.util.List;
import java.util.Map;

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.task.LearningTaskService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.study.TbLearningTask;
import org.xyzh.common.dto.study.TbTaskUser;
import org.xyzh.common.vo.TaskItemVO;
import org.xyzh.common.vo.TaskVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;



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

    @Autowired
    private LearningTaskService learningTaskService;

    /**
     * 获取任务列表
     */
    @GetMapping("/list")
    public ResultDomain<TbLearningTask> getTaskList(TbLearningTask filter) {
        return learningTaskService.getTaskList(filter);
    }

    /**
     * 获取任务列表分页
     */
    @PostMapping("/page")
    public ResultDomain<TbLearningTask> getTaskPage(@RequestBody PageRequest<TbLearningTask> pageRequest) {
        TbLearningTask filter = pageRequest.getFilter();
        PageParam pageParam = pageRequest.getPageParam();
        return learningTaskService.getTaskPage(filter, pageParam);
    }

    /**
     * 根据ID获取任务详情
     */
    @GetMapping("/{taskID}")
    public ResultDomain<TaskVO> getTaskById(@PathVariable("taskID") String taskID) {
        return learningTaskService.getTaskById(taskID);
    }

    @GetMapping("/{taskID}/user")
    public ResultDomain<TaskVO> getTaskUser(@PathVariable("taskID") String taskID) {
        return learningTaskService.getTaskUser(taskID);
    }

    /**
     * 创建任务
     */
    @PostMapping("/task")
    public ResultDomain<TaskVO> createTask(@RequestBody TaskVO taskVO) {
        return learningTaskService.createTask(taskVO);
    }

    /**
     * 更新任务
     */
    @PutMapping("/task")
    public ResultDomain<TaskVO> updateTask(@RequestBody TaskVO taskVO) {
        return learningTaskService.updateTask(taskVO);
    }

    /**
     * 删除任务
     */
    @DeleteMapping("/task")
    public ResultDomain<Boolean> deleteTask(@PathVariable("taskID") String taskID) {
        return learningTaskService.deleteTask(taskID);
    }

    /**
     * 更新任务状态
     */
    @PutMapping("/status")
    public ResultDomain<TbLearningTask> updateTaskStatus(@RequestBody TbLearningTask task) {
        return learningTaskService.updateTaskStatus(task.getTaskID(), task.getStatus());
    }

    /**
     * 获取任务用户列表
     */
    @GetMapping("/{taskID}/users")
    public ResultDomain<TaskItemVO> getTaskUsers(@PathVariable("taskID") String taskID) {
        return learningTaskService.getTaskUsers(taskID);
    }

    /**
     * 批量添加任务用户
     */
    @PostMapping("/{taskID}/users")
    public ResultDomain<TbTaskUser> addTaskUser(@PathVariable("taskID") String taskID, @RequestBody Map<String, List<String>> map) {
        List<String> userIDs = map.get("userIDs");
        return learningTaskService.addTaskUser(taskID, userIDs);
    }

    @DeleteMapping("/{taskID}/users")
    public ResultDomain<Boolean> removeTaskUser(@PathVariable("taskID") String taskID, @RequestBody Map<String, List<String>> map) {
        List<String> userIDs = map.get("userIDs");
        return learningTaskService.removeTaskUser(taskID, userIDs);
    }

    /**
     * 分配任务给用户
     */
    @PostMapping("/{taskID}/assign")
    public ResultDomain<TbTaskUser> assignTaskToUser(@PathVariable("taskID") String taskID, @RequestParam String userID) {
        return null;
        // return learningTaskService.assignTaskToUser(taskID, userID);
    }

    /**
     * 从用户移除任务
     */
    @DeleteMapping("/{taskID}/user/{userID}")
    public ResultDomain<Boolean> removeTaskFromUser(@PathVariable("taskID") String taskID, @PathVariable("userID") String userID) {
        return null;
        // return learningTaskService.removeTaskFromUser(taskID, userID);
    }

    /**
     * 用户获取个人任务列表（用户视角）
     */
    @PostMapping("/users/page")
    public ResultDomain<TbLearningTask> getUserTaskPage(@RequestBody PageRequest<TaskItemVO> pageRequest) {
        TaskItemVO filter = pageRequest.getFilter();
        PageParam pageParam = pageRequest.getPageParam();
        return learningTaskService.getUserTaskPage(filter, pageParam);
    }
    

    /**
     *  用户整体学习进度
     */
    @PostMapping("/user/progress/{userID}")
    public ResultDomain<TaskVO> getUserProgress(@PathVariable("userID") String userID) {
        return learningTaskService.getUserProgress(userID);
    }
    

    
}
