package com.seer.teach.admin.controller;

import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.cache.dto.CorrectResultDTO;
import com.seer.teach.common.service.CommonFileService;
import com.seer.teach.common.utils.CommonUtils;
import com.seer.teach.teacher.common.resp.BboxResp;
import com.seer.teach.teacher.service.CorrectService;
import com.seer.teach.teacher.service.GradingTasksService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import jakarta.validation.constraints.NotNull;

import static cn.dev33.satoken.stp.StpUtil.getLoginIdAsInt;

/**
 * AiApiKey控制器
 *
 * @author ZS
 * @since 2025-10-27
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/teacher/ai/correct")
@Tag(name = "管理端 - 批改测试")
public class AdminCorrectController {

    private final CommonFileService commonFileService;

    private final GradingTasksService gradingTasksService;

    private final CorrectService correctService;

    @LogPrint
    @Operation(summary = "上传批改图片")
    @PostMapping("/uploadImage")
    public ResultBean<String> aiCorrect(
            @RequestParam("file") @NotNull(message = "文件不能为空") MultipartFile file) {
        // 校验图片类型是否合法
        CommonUtils.isIllegalImageType(file);
        int userId = getLoginIdAsInt();
        String url = commonFileService.uploadAiCorrectImage(userId, file);
        return ResultBean.success(url);
    }

    @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @LogPrint
    @Operation(summary = "批改")
    public Flux<CorrectResultDTO> correct(@RequestParam String imageUrl) {
        return gradingTasksService.correct(imageUrl);
    }


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

}