package com.writeoff.module.ocr.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.module.ocr.dto.BankCardOcrRequest;
import com.writeoff.module.ocr.dto.BankCardOcrResponse;
import com.writeoff.module.ocr.dto.IdCardOcrRequest;
import com.writeoff.module.ocr.dto.IdCardOcrResponse;
import com.writeoff.module.ocr.service.BaiduBankCardOcrService;
import com.writeoff.module.ocr.service.BaiduIdCardOcrService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.PermissionDomain;
import com.writeoff.security.RequirePermission;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/platform/ocr")
public class PlatformOcrController {
    private final BaiduIdCardOcrService idCardOcrService;
    private final BaiduBankCardOcrService bankCardOcrService;

    public PlatformOcrController(BaiduIdCardOcrService idCardOcrService, BaiduBankCardOcrService bankCardOcrService) {
        this.idCardOcrService = idCardOcrService;
        this.bankCardOcrService = bankCardOcrService;
    }

    @PostMapping("/id-card")
    @RequirePermission(
            value = "platform.ocr.idcard",
            domain = PermissionDomain.PLATFORM,
            dataScope = DataScopeType.GLOBAL_READONLY,
            auditAction = "PLATFORM_OCR_ID_CARD"
    )
    public ApiResponse<IdCardOcrResponse> idCard(@RequestBody @Validated IdCardOcrRequest request) {
        return ApiResponse.success(idCardOcrService.recognize(request.getObjectKey(), request.getIdCardSide()));
    }

    @PostMapping("/bank-card")
    @RequirePermission(
            value = "platform.ocr.bankcard",
            domain = PermissionDomain.PLATFORM,
            dataScope = DataScopeType.GLOBAL_READONLY,
            auditAction = "PLATFORM_OCR_BANK_CARD"
    )
    public ApiResponse<BankCardOcrResponse> bankCard(@RequestBody @Validated BankCardOcrRequest request) {
        return ApiResponse.success(bankCardOcrService.recognize(request.getObjectKey()));
    }
}

