package com.dora.controller;

import com.dora.common.Result;
import com.dora.util.CertificateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 证书状态检查控制器
 */
@Slf4j
@RestController
@RequestMapping("/user/certificate")
@Tag(name = "证书管理", description = "证书文件状态检查接口")
public class CertificateController {

    @Autowired
    private CertificateUtil certificateUtil;

    /**
     * 检查证书状态
     */
    @GetMapping("/status")
    @Operation(summary = "检查证书状态", description = "检查微信支付证书文件的状态")
    public Result<Map<String, Object>> checkCertificateStatus() {
        try {
            Map<String, Object> status = new HashMap<>();
            
            // 检查证书是否存在
            boolean exists = certificateUtil.isCertificateExists();
            status.put("exists", exists);
            
            // 检查证书权限
            boolean hasPermission = certificateUtil.checkCertificatePermissions();
            status.put("hasPermission", hasPermission);
            
            // 验证证书格式
            boolean validFormat = certificateUtil.validateCertificateFormat();
            status.put("validFormat", validFormat);
            
            // 获取证书文件大小
            long fileSize = certificateUtil.getCertificateFileSize();
            status.put("fileSize", fileSize);
            
            // 获取证书路径
            String certUrl = certificateUtil.getCertUrl();
            status.put("certUrl", certUrl);
            
            // 计算整体状态
            boolean overallStatus = exists && hasPermission && validFormat;
            status.put("overallStatus", overallStatus);
            
            if (overallStatus) {
                log.info("证书状态检查通过");
                return Result.success(status);
            } else {
                log.warn("证书状态检查未通过: exists={}, hasPermission={}, validFormat={}", 
                    exists, hasPermission, validFormat);
                return Result.error("证书状态检查未通过", status);
            }
            
        } catch (Exception e) {
            log.error("检查证书状态时发生错误: {}", e.getMessage(), e);
            return Result.error("检查证书状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取证书详细信息
     */
    @GetMapping("/info")
    @Operation(summary = "获取证书详细信息", description = "获取微信支付证书文件的详细信息")
    public Result<String> getCertificateInfo() {
        try {
            String info = certificateUtil.getCertificateInfo();
            return Result.success(info);
        } catch (Exception e) {
            log.error("获取证书信息时发生错误: {}", e.getMessage(), e);
            return Result.error("获取证书信息失败: " + e.getMessage());
        }
    }

    /**
     * 测试证书加载
     */
    @GetMapping("/test")
    @Operation(summary = "测试证书加载", description = "测试微信支付证书文件是否可以正常加载")
    public Result<Boolean> testCertificateLoading() {
        try {
            boolean valid = certificateUtil.validateCertificateFormat();
            
            if (valid) {
                log.info("证书加载测试通过");
                return Result.success(true);
            } else {
                log.warn("证书加载测试失败");
                return Result.error("证书加载测试失败");
            }
            
        } catch (Exception e) {
            log.error("测试证书加载时发生错误: {}", e.getMessage(), e);
            return Result.error("测试证书加载失败: " + e.getMessage());
        }
    }
} 