package com.hotwj.platform.securityManagement.trainingAnswer.controller;

import com.hotwj.platform.securityManagement.trainingAnswer.domain.bo.SubmitTrainingAnswerBo;
import com.hotwj.platform.securityManagement.trainingAnswer.domain.bo.TrainingAnswerRecordBo;
import com.hotwj.platform.securityManagement.trainingAnswer.domain.vo.HotTrainingAnswerRecordVo;
import com.hotwj.platform.securityManagement.trainingAnswer.domain.vo.HotTrainingWrongQuestionReviewVo;
import com.hotwj.platform.securityManagement.trainingAnswer.service.IHotTrainingAnswerRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 培训课程答题详情记录
 *
 * @author shihongwei
 * @date 2026-02-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/courseAnswer/trainingAnswerRecord")
@Tag(name = "培训课程答题详情记录", description = "培训课程答题详情记录管理")
public class HotTrainingAnswerRecordController extends BaseController {

    private final IHotTrainingAnswerRecordService hotTrainingAnswerRecordService;

    /**
     * 查询培训课程答题详情记录列表
     */
    //@SaCheckPermission("courseAnswer:trainingAnswerRecord:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询培训课程答题详情记录列表")
    public TableDataInfo<HotTrainingAnswerRecordVo> list(TrainingAnswerRecordBo bo, PageQuery pageQuery) {
        return hotTrainingAnswerRecordService.queryPageList(bo, pageQuery);
    }

    //@SaCheckPermission("courseAnswer:trainingAnswerRecord:list")
    @GetMapping("/studyDetail")
    @Operation(summary = "查询培训计划答题明细（按培训+用户）")
    public R<List<HotTrainingAnswerRecordVo>> studyDetail(
            @RequestParam Long companyId,
            @RequestParam Long trainingId,
            @RequestParam String userId
    ) {
        return R.ok(hotTrainingAnswerRecordService.queryStudyDetailByTrainingAndUser(companyId, trainingId, userId));
    }

    /**
     * 导出培训课程答题详情记录列表
     */
    //@SaCheckPermission("courseAnswer:trainingAnswerRecord:export")
    @Log(title = "培训课程答题详情记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出培训课程答题详情记录列表")
    public void export(TrainingAnswerRecordBo bo, HttpServletResponse response) {
        List<HotTrainingAnswerRecordVo> list = hotTrainingAnswerRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "培训课程答题详情记录", HotTrainingAnswerRecordVo.class, response);
    }

    /**
     * 获取培训课程答题详情记录详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("courseAnswer:trainingAnswerRecord:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取培训课程答题详情记录详细信息")
    public R<HotTrainingAnswerRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @Parameter(name = "id", description = "主键", required = true, example = "1")
                                     @PathVariable Long id) {
        return R.ok(hotTrainingAnswerRecordService.queryById(id));
    }

    /**
     * 新增培训课程答题详情记录
     */
    //@SaCheckPermission("courseAnswer:trainingAnswerRecord:add")
    @Log(title = "培训课程答题详情记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增培训课程答题详情记录")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TrainingAnswerRecordBo bo) {
        return toAjax(hotTrainingAnswerRecordService.insertByBo(bo));
    }

    /**
     * 修改培训课程答题详情记录
     */
    //@SaCheckPermission("courseAnswer:trainingAnswerRecord:edit")
    @Log(title = "培训课程答题详情记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改培训课程答题详情记录")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TrainingAnswerRecordBo bo) {
        return toAjax(hotTrainingAnswerRecordService.updateByBo(bo));
    }

    /**
     * 删除培训课程答题详情记录
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("courseAnswer:trainingAnswerRecord:remove")
    @Log(title = "培训课程答题详情记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除培训课程答题详情记录")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @Parameter(name = "ids", description = "主键串", required = true, example = "1,2,3")
                          @PathVariable Long[] ids) {
        return toAjax(hotTrainingAnswerRecordService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量提交某次答题的整套题目作答记录
     */
    //@SaCheckPermission("courseAnswer:trainingAnswerRecord:add")
    @Log(title = "培训课程答题详情记录-批量提交", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/submit")
    @Operation(summary = "批量提交某次答题的整套题目作答记录（支持随堂练习/培训计划答题）")
    public R<Void> submit(@RequestBody @Validated(AddGroup.class) SubmitTrainingAnswerBo bo) {
        return toAjax(hotTrainingAnswerRecordService.submitWithSignature(bo));
    }

    @GetMapping("/wrongQuestion/review")
    @Operation(summary = "查询错题回顾（当前用户）")
    public R<List<HotTrainingWrongQuestionReviewVo>> wrongQuestionReview(TrainingAnswerRecordBo bo) {
        Long companyId = LoginHelper.getCompanyId();
        String userId = LoginHelper.getBusinessUserId();
        if (StringUtils.isBlank(userId)) {
            userId = LoginHelper.getUserIdStr();
        }
        List<HotTrainingWrongQuestionReviewVo> result = hotTrainingAnswerRecordService.queryWrongQuestionReview(companyId, userId, bo);
        return R.ok(result);
    }
}
