package com.sqx.modules.face.controller;

import com.sqx.modules.face.entity.FaceAuthRequest;
import com.sqx.modules.face.service.FaceAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(value = "人脸识别", tags = {"人脸识别"})
@RestController
@RequestMapping("/api/face-auth")
public class FaceAuthController {

    @Autowired
    private FaceAuthService faceAuthService;

    // 启动核身任务，返回跳转 URL 给小程序
    @PostMapping("/start")
    @ApiOperation("启动核身任务")
    public ResponseEntity<String> startFaceAuth(@RequestBody FaceAuthRequest request) {
        String authUrl = faceAuthService.createAuthUrl(request.getName(), request.getIdCard(), request.getUserId());
        return ResponseEntity.ok(authUrl);
    }

    // 接收腾讯回调通知核身完成（可配置在腾讯云控制台）
    @PostMapping("/callback")
    @ApiOperation("接收腾讯回调通知核身完成")
    public ResponseEntity<String> handleCallback(@RequestBody Map<String, Object> payload) {
        faceAuthService.handleCallback(payload);
        return ResponseEntity.ok("success");
    }

    // 主动查询核身结果（用 bizToken）仅保留三天
    @GetMapping("/result")
    @ApiOperation("主动查询核身结果")
    public ResponseEntity<Object> getAuthResult(@RequestParam String bizToken) {
        return ResponseEntity.ok(faceAuthService.getAuthResult(bizToken));
    }
}

