package cn.iocoder.yudao.module.course.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.course.entity.Process;
import cn.iocoder.yudao.module.course.service.ProcessService;
import cn.iocoder.yudao.module.course.vo.ProcessNewVO;
import cn.iocoder.yudao.module.course.vo.ProcessVO;
import cn.iocoder.yudao.module.system.service.dict.DictDataService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "学员课程进度")
@RestController
@RequestMapping("/process")
@Validated
public class ProcessSmallProgramController {

    @Resource
    private ProcessService processService;

    @Resource
    private DictDataService dictDataService;


    /**
     * 学员课程进度分页查询
     *
     * @param pageReqVO {@link ProcessVO}
     * @param pageNo    分页参数
     * @param pageSize  分页参数
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage < ?>>
     * @author PQZ
     * @date 15:33 2025/2/17
     **/
    @GetMapping("/page")
    @Operation(summary = "学员课程进度分页")
    @TenantIgnore
    public CommonResult<IPage<?>> getDlDriveSchoolCoachPage(ProcessVO pageReqVO,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<ProcessVO> page = new Page<>(pageNo, pageSize);
        return success(processService.pageProcess(page, pageReqVO));
    }

    /**
     * 修改学员课程进度
     *
     * @param process {@link Process}
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     */
    @PutMapping("/updateProcess")
    @Operation(summary = "修改学员课程进度")
    @TenantIgnore
    public CommonResult updateProcess(@RequestBody Process process) {
        processService.updateProcess(process);
        return CommonResult.ok();
    }

    /**
     * 通过学员id和课程id查询学员课程进度
     *
     * @param userId   学员id
     * @param courseId 课程id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.util.List < cn.iocoder.yudao.module.course.vo.ProcessVO>>
     */
    @GetMapping("/getAllByUserIdAndCourseId")
    @TenantIgnore
    public CommonResult<?> getAllByUserIdAndCourseId(Long userId, String courseId) {
        return success(processService.getAllByUserIdAndCourseId(userId, courseId));
    }

    /**
     * 根据userId查询考试列表
     */
    @GetMapping("/getExamListByUserId")
    @TenantIgnore
    public CommonResult<?> getExamListByUserId(Long userId) {
        return success(processService.getExamListByUserIdTest(userId));
    }

    /**
     * 根据userId查询考试列表
     */
    @GetMapping("/getExamListByUserIdNew")
    @TenantIgnore
    public CommonResult<?> getExamListByUserIdNew(Long userId) {
        return success(processService.getExamListByUserIdTestNew(userId));
    }

    /**
     * 通过学员id和课程id查询学员课程进度
     *
     * @param courseId 课程id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.util.List < cn.iocoder.yudao.module.course.vo.ProcessVO>>
     */
    @TenantIgnore
    @GetMapping("/getAllByCourseId")
    public CommonResult<?> getAllByCourseId(String courseId) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return success(processService.list(Wrappers.lambdaQuery(Process.class)
                .eq(Process::getUserId, loginUserId)
                .eq(Process::getCourseId, courseId)));
    }

    /**
     * 教练自招学员，自动分配教练
     */
    @PostMapping("/autoAssignCoach")
    public CommonResult<Boolean> autoAssignCoach(@RequestBody ProcessNewVO requset) {
        processService.autoAssignCoach(requset);
        return success(true);
    }

    /**
     * 获取打卡开关值
     */
    @GetMapping("/getClockSwitchValue")
    public CommonResult<String> getClockSwitchValue(String type, String label) {
        return success(dictDataService.getValueByTypeAndLabel(type,  label));
    }



}
