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.DocumentExtractTaskQueryRequest;
import com.writeoff.module.ocr.dto.DocumentExtractTaskQueryResponse;
import com.writeoff.module.ocr.dto.DocumentExtractTaskSubmitRequest;
import com.writeoff.module.ocr.dto.DocumentExtractTaskSubmitResponse;
import com.writeoff.module.ocr.dto.IdCardOcrRequest;
import com.writeoff.module.ocr.dto.IdCardOcrResponse;
import com.writeoff.module.ocr.dto.MultipleInvoiceOcrRequest;
import com.writeoff.module.ocr.dto.MultipleInvoiceOcrResponse;
import com.writeoff.module.ocr.service.BaiduBankCardOcrService;
import com.writeoff.module.ocr.service.BaiduDocumentExtractService;
import com.writeoff.module.ocr.service.BaiduIdCardOcrService;
import com.writeoff.module.ocr.service.BaiduMultipleInvoiceOcrService;
import com.writeoff.security.DataScopeType;
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/ocr")
public class OcrController {
    private final BaiduMultipleInvoiceOcrService multipleInvoiceOcrService;
    private final BaiduIdCardOcrService idCardOcrService;
    private final BaiduBankCardOcrService bankCardOcrService;
    private final BaiduDocumentExtractService documentExtractService;

    public OcrController(
            BaiduMultipleInvoiceOcrService multipleInvoiceOcrService,
            BaiduIdCardOcrService idCardOcrService,
            BaiduBankCardOcrService bankCardOcrService,
            BaiduDocumentExtractService documentExtractService
    ) {
        this.multipleInvoiceOcrService = multipleInvoiceOcrService;
        this.idCardOcrService = idCardOcrService;
        this.bankCardOcrService = bankCardOcrService;
        this.documentExtractService = documentExtractService;
    }

    @PostMapping("/multiple-invoice")
    @RequirePermission(value = "meeting.material.save", dataScope = DataScopeType.MEETING_MODULE, auditAction = "OCR_MULTIPLE_INVOICE")
    public ApiResponse<MultipleInvoiceOcrResponse> multipleInvoice(@RequestBody @Validated MultipleInvoiceOcrRequest request) {
        return ApiResponse.success(multipleInvoiceOcrService.recognize(request.getObjectKey()));
    }

    @PostMapping("/id-card")
    @RequirePermission(value = "ocr.idcard", dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "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 = "ocr.bankcard", dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "OCR_BANK_CARD")
    public ApiResponse<BankCardOcrResponse> bankCard(@RequestBody @Validated BankCardOcrRequest request) {
        return ApiResponse.success(bankCardOcrService.recognize(request.getObjectKey()));
    }

    @PostMapping("/document-extract/task")
    @RequirePermission(value = "meeting.material.save", dataScope = DataScopeType.MEETING_MODULE, auditAction = "OCR_DOCUMENT_EXTRACT_SUBMIT")
    public ApiResponse<DocumentExtractTaskSubmitResponse> submitDocumentExtractTask(@RequestBody @Validated DocumentExtractTaskSubmitRequest request) {
        return ApiResponse.success(documentExtractService.submitTask(request));
    }

    @PostMapping("/document-extract/query-task")
    @RequirePermission(value = "meeting.material.save", dataScope = DataScopeType.MEETING_MODULE, auditAction = "OCR_DOCUMENT_EXTRACT_QUERY")
    public ApiResponse<DocumentExtractTaskQueryResponse> queryDocumentExtractTask(@RequestBody @Validated DocumentExtractTaskQueryRequest request) {
        return ApiResponse.success(documentExtractService.queryTask(request.getTaskId()));
    }
}
