package com.nbport.zgwl.seal.controller;

import com.alibaba.fastjson2.JSON;
import com.nbport.zgwl.common.R;
import com.nbport.zgwl.seal.handler.SealWebSocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;

import java.util.Map;

/**
 * 签章平台回调接口控制器
 *
 * @author xiaofengzhou
 */
@RestController
@RequestMapping("/seal/callback")
public class SealCallbackController {

    private static final Logger log = LoggerFactory.getLogger(SealCallbackController.class);

    @Autowired
    private SealWebSocketHandler webSocketHandler;

    @PostMapping("/login")
    public R<String> loginCallback(@RequestBody Map<String, Object> params) {
        log.info("收到身份认证回调: {}", JSON.toJSONString(params));

        try {
            String requestId = (String) params.get("requestId");
            webSocketHandler.sendCallbackMessage(requestId, "login", params);
            return R.success("回调接收成功");
        } catch (Exception e) {
            log.error("处理身份认证回调失败", e);
            return R.error("回调处理失败: " + e.getMessage());
        }
    }

    @PostMapping("/sign")
    public R<String> signCallback(@RequestBody Map<String, Object> params) {
        log.info("收到签署回调: {}", JSON.toJSONString(params));

        try {
            String contractId = (String) params.get("contractId");
            String requestId = (String) params.get("requestId");
            webSocketHandler.sendCallbackMessage(requestId, "sign", params);
            webSocketHandler.sendCallbackMessage(contractId, "contract_status", params);
            return R.success("回调接收成功");
        } catch (Exception e) {
            log.error("处理签署回调失败", e);
            return R.error("回调处理失败: " + e.getMessage());
        }
    }

    @PostMapping("/presign")
    public R<String> presignCallback(@RequestBody Map<String, Object> params) {
        log.info("收到预盖章回调: {}", JSON.toJSONString(params));

        try {
            String contractId = (String) params.get("contractId");
            String requestId = (String) params.get("requestId");
            webSocketHandler.sendCallbackMessage(requestId, "presign", params);
            webSocketHandler.sendCallbackMessage(contractId, "contract_status", params);
            return R.success("回调接收成功");
        } catch (Exception e) {
            log.error("处理预盖章回调失败", e);
            return R.error("回调处理失败: " + e.getMessage());
        }
    }

    @PostMapping("/trusteeship")
    public R<String> trusteeshipCallback(@RequestBody Map<String, Object> params) {
        log.info("收到托管章回调: {}", JSON.toJSONString(params));

        try {
            String requestId = (String) params.get("requestId");
            webSocketHandler.sendCallbackMessage(requestId, "trusteeship", params);
            return R.success("回调接收成功");
        } catch (Exception e) {
            log.error("处理托管章回调失败", e);
            return R.error("回调处理失败: " + e.getMessage());
        }
    }

    @PostMapping("/signQrCode")
    public R<String> signQrCodeCallback(@RequestBody Map<String, Object> params) {
        log.info("收到签名二维码回调: {}", JSON.toJSONString(params));

        try {
            String requestId = (String) params.get("requestId");
            webSocketHandler.sendCallbackMessage(requestId, "signQrCode", params);
            return R.success("回调接收成功");
        } catch (Exception e) {
            log.error("处理签名二维码回调失败", e);
            return R.error("回调处理失败: " + e.getMessage());
        }
    }

    @GetMapping("/poll/{requestId}")
    public R<Map<String, Object>> pollCallback(@PathVariable String requestId) {
        Map<String, Object> callbackData = webSocketHandler.getCallbackData(requestId);
        return R.success(callbackData);
    }
}
