package com.sqx.modules.face.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.face.dao.FaceAuthRequestDao;
import com.sqx.modules.face.entity.FaceAuthRequest;
import com.sqx.modules.face.service.FaceAuthService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthRequest;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthResponse;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoRequest;
import com.tencentcloudapi.faceid.v20180301.models.GetDetectInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class FaceAuthServiceImpl extends ServiceImpl<FaceAuthRequestDao, FaceAuthRequest> implements FaceAuthService {

    @Autowired
    private CommonInfoService commonInfoService;

    private static final String SECRET_ID = "你的SecretId";
    private static final String SECRET_KEY = "你的SecretKey";
    private static final String REGION = "ap-beijing";
    private static final String RULE_ID = "你的核身规则ID";

    public String createAuthUrl(String name, String idCard, String userId) {
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);

            FaceidClient client = new FaceidClient(cred, REGION, new ClientProfile());

            DetectAuthRequest req = new DetectAuthRequest();
            req.setName(name);
            req.setIdCard(idCard);
            req.setRuleId(RULE_ID);
            req.setRedirectUrl("https://yourdomain.com/face-result"); // 用户核身完成跳转地址
            req.setExtra(userId); // 自定义字段

            DetectAuthResponse resp = client.DetectAuth(req);
            return resp.getUrl(); // 返回前端跳转
        } catch (Exception e) {
            throw new RuntimeException("创建核身任务失败", e);
        }
    }

    public void handleCallback(Map<String, Object> payload) {
        // 腾讯人脸核身完成后会推送回调数据到你配置的 URL
        String bizToken = (String) payload.get("BizToken");
        System.out.println("收到回调，bizToken = " + bizToken);

        // 你可以在这里直接调用 getAuthResult 查询结果并入库
        Object result = getAuthResult(bizToken);
        System.out.println("核身结果：" + result);
        // 保存到数据库等
    }

    public Object getAuthResult(String bizToken) {
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
            FaceidClient client = new FaceidClient(cred, REGION, new ClientProfile());

            GetDetectInfoRequest req = new GetDetectInfoRequest();
            req.setBizToken(bizToken);
            req.setInfoType("1"); // 基础信息
            GetDetectInfoResponse resp = client.GetDetectInfo(req);

            return resp.getDetectInfo(); // 是 JSON 字符串，可以用 Jackson 解析
        } catch (Exception e) {
            throw new RuntimeException("获取核身结果失败", e);
        }
    }
}
