package com.ruoyi.lsfx.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.lsfx.client.CreditParseClient;
import com.ruoyi.lsfx.domain.response.CreditParseInvokeResponse;
import com.ruoyi.lsfx.exception.LsfxApiException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "征信解析接口测试", description = "用于测试征信解析接口")
@Anonymous
@RestController
@RequestMapping("/lsfx/credit")
public class CreditParseController {

    private static final String DEFAULT_MODEL = "LXCUSTALL";

    @Resource
    private CreditParseClient creditParseClient;

    @Operation(summary = "解析征信HTML", description = "传入征信HTML远程地址并调用外部解析服务")
    @PostMapping("/parse")
    public AjaxResult parse(@Parameter(description = "征信HTML远程访问地址") @RequestParam("remotePath") String remotePath,
                            @Parameter(description = "模型编码，默认LXCUSTALL") @RequestParam(required = false) String model) {
        if (StringUtils.isBlank(remotePath)) {
            return AjaxResult.error("征信HTML远程地址不能为空");
        }

        String actualModel = StringUtils.isBlank(model) ? DEFAULT_MODEL : model;

        try {
            CreditParseInvokeResponse response = creditParseClient.parse(actualModel, remotePath);
            return AjaxResult.success(response);
        } catch (LsfxApiException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("征信解析失败：" + e.getMessage());
        }
    }
}
