package com.seer.teach.teacher.controller.app;

import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogOperation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.ModuleEnum;
import com.seer.teach.teacher.common.req.GradingTasksPageListReq;
import com.seer.teach.teacher.common.req.QuestionsCorrectReq;
import com.seer.teach.teacher.common.resp.BboxResp;
import com.seer.teach.teacher.common.resp.GradingTasksPageListResp;
import com.seer.teach.teacher.common.resp.QuestionsCorrectDTO;
import com.seer.teach.teacher.service.CorrectService;
import com.seer.teach.teacher.service.GradingTasksService;
import com.seer.teach.teacher.service.registry.questionsCorrect.QuestionsCorrectHandler;
import com.seer.teach.teacher.service.registry.questionsCorrect.QuestionsCorrectHandlerRegistry;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.validation.constraints.NotNull;

/**
 * TV端AI批改控制器，提供试卷查询、题目识别与智能批改功能。
 */
@Slf4j
@Validated
@RestController
@EncryptionAnnotation
@DecryptionAnnotation
@RequiredArgsConstructor
@Tag(name = "APP - Ai批改")
@RequestMapping("/app/correct")
public class CorrectController {

    private final QuestionsCorrectHandlerRegistry questionsCorrectHandlerRegistry;

    private final GradingTasksService gradingTasksService;

    private final CorrectService correctService;

    @PostMapping("/getHistory")
    @LogPrint
    @Operation(summary = "TV端分页查询试卷")
    public ResultBean<PageListBean<GradingTasksPageListResp>> getGradingTasks(@RequestBody @Validated GradingTasksPageListReq param) {
        PageListBean<GradingTasksPageListResp> gradingTasks = gradingTasksService.getGradingTasks(param);
        return ResultBean.success(gradingTasks);
    }

    @LogPrint
    @Operation(summary = "批改")
    @PostMapping("/questionCorrect")
    @LogOperation(module = ModuleEnum.SEER_TEACHER, subModule = "AI批改")
    public ResultBean<QuestionsCorrectDTO> correct(@RequestBody @Validated QuestionsCorrectReq params) {
        QuestionsCorrectHandler handler = questionsCorrectHandlerRegistry.getHandler(params.getType());
        if (handler == null) {
            log.warn("未找到对应处理器：type={}", params.getType());
            return ResultBean.error();
        }
        QuestionsCorrectDTO results = handler.handler(params);
        return ResultBean.success(results);
    }

    @PostMapping("/bbox")
    @LogPrint
    @Operation(summary = "框题")
    public ResultBean<BboxResp> aiCorrect(
            @RequestParam("file") @NotNull(message = "文件不能为空") MultipartFile file) {
        BboxResp ocr = correctService.ocr(file);
        return ResultBean.success(ocr);
    }
}
