package cn.iocoder.yudao.module.ocr.controller.admin.ocr;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.ocr.controller.admin.ocr.vo.VehicleLicenseRespVO;
import cn.iocoder.yudao.module.ocr.convert.ocr.OcrConvert;
import cn.iocoder.yudao.module.ocr.framework.ocr.core.result.VehicleLicenseResult;
import cn.iocoder.yudao.module.ocr.service.ocr.OcrService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.io.IOException;

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

/**
 * OCR 识别控制器
 */
@Tag(name = "管理后台 - OCR 识别")
@RestController
@RequestMapping("/ocr")
@Validated
@Slf4j
public class OcrController {

    @Resource
    private OcrService ocrService;

    @PostMapping("/vehicle-license")
    @Operation(summary = "识别行驶证")
    @PreAuthorize("@ss.hasPermission('ocr:vehicle-license:recognize')")
    public CommonResult<VehicleLicenseRespVO> recognizeVehicleLicense(
            @Parameter(description = "行驶证图片文件", required = true)
            @RequestParam("file") MultipartFile file,
            @Parameter(description = "OCR 厂商（可选，默认使用配置的厂商）")
            @RequestParam(value = "provider", required = false) String provider) throws IOException {

        // 读取图片数据
        byte[] imageData = file.getBytes();

        // 调用识别服务
        VehicleLicenseResult result = ocrService.recognizeVehicleLicense(imageData, provider);

        // 转换并返回
        return success(OcrConvert.INSTANCE.convert(result));
    }

}
