package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.vo.MainExamApplyRecordVo;
import org.dromara.main.domain.vo.MainExamApplyVo;
import org.dromara.main.service.IMainExamApplyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 测评申请Controller
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/main/exam-apply")
public class MainExamApplyController extends BaseController {

    private final IMainExamApplyService examApplyService;

    /**
     * 创建测评申请
     */
    @SaIgnore
    @Log(title = "创建测评申请", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R<MainExamApplyVo> createExamApply(@RequestParam(required = false) Long evaluationId, @RequestParam Long studentId) {
        log.info("收到创建测评申请请求，evaluationId: {}, studentId: {}", evaluationId, studentId);
        
        if (evaluationId == null) {
            return R.fail("测评ID不能为空");
        }
        
        MainExamApplyVo result = examApplyService.createExamApply(evaluationId, studentId);
        return R.ok(result);
    }

    /**
     * 根据ID查询测评申请
     */
    @GetMapping("/{id}")
    public R<MainExamApplyVo> getInfo(@PathVariable Long id) {
        MainExamApplyVo result = examApplyService.queryById(id);
        return R.ok(result);
    }

    /**
     * 查询学生测评记录列表
     */
    @SaIgnore
    @GetMapping("/record/list")
    public R<List<MainExamApplyRecordVo>> recordList(@RequestParam Long studentId) {
        return R.ok(examApplyService.queryRecordList(studentId));
    }

    /**
     * 更新申请状态
     */
    @Log(title = "更新测评申请状态", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/status")
    public R<Void> updateStatus(@PathVariable Long id, @RequestParam String status) {
        Boolean result = examApplyService.updateApplyStatus(id, status);
        return toAjax(result);
    }
}
