package cn.meowrain.aioj.backend.aiservice.client;

import cn.meowrain.aioj.backend.aiservice.grpc.*;
import io.grpc.StatusRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * AI Service gRPC 客户端封装类
 */
@Slf4j
@Component
public class AIServiceGrpcClient {

    private final AIServiceGrpc.AIServiceBlockingStub blockingStub;
    private final AIServiceGrpc.AIServiceStub asyncStub;

    public AIServiceGrpcClient(
            @Qualifier("aiServiceBlockingStub") AIServiceGrpc.AIServiceBlockingStub blockingStub,
            @Qualifier("aiServiceStub") AIServiceGrpc.AIServiceStub asyncStub) {
        this.blockingStub = blockingStub;
        this.asyncStub = asyncStub;
    }

    /**
     * 分析代码
     */
    public AnalyzeCodeResponse analyzeCode(String code, String language, String questionId, String userId) {
        try {
            log.info("Calling gRPC analyzeCode for language: {}, questionId: {}", language, questionId);

            AnalyzeCodeRequest request = AnalyzeCodeRequest.newBuilder()
                    .setCode(code)
                    .setLanguage(language)
                    .setQuestionId(questionId)
                    .setUserId(userId)
                    .build();

            AnalyzeCodeResponse response = blockingStub.analyzeCode(request);

            log.info("gRPC analyzeCode response: success={}", response.getSuccess());
            return response;

        } catch (StatusRuntimeException e) {
            log.error("gRPC analyzeCode failed: {}", e.getStatus(), e);
            return AnalyzeCodeResponse.newBuilder()
                    .setSuccess(false)
                    .setMessage("gRPC 调用失败: " + e.getStatus().getDescription())
                    .build();
        }
    }

    /**
     * 优化代码
     */
    public OptimizeCodeResponse optimizeCode(String code, String language, String optimizationType) {
        try {
            log.info("Calling gRPC optimizeCode for language: {}, type: {}", language, optimizationType);

            OptimizeCodeRequest request = OptimizeCodeRequest.newBuilder()
                    .setCode(code)
                    .setLanguage(language)
                    .setOptimizationType(optimizationType)
                    .build();

            OptimizeCodeResponse response = blockingStub.optimizeCode(request);

            log.info("gRPC optimizeCode response: success={}", response.getSuccess());
            return response;

        } catch (StatusRuntimeException e) {
            log.error("gRPC optimizeCode failed: {}", e.getStatus(), e);
            return OptimizeCodeResponse.newBuilder()
                    .setSuccess(false)
                    .setMessage("gRPC 调用失败: " + e.getStatus().getDescription())
                    .build();
        }
    }

    /**
     * 生成测试用例
     */
    public GenerateTestCasesResponse generateTestCases(String code, String language, String problemDescription) {
        try {
            log.info("Calling gRPC generateTestCases for language: {}", language);

            GenerateTestCasesRequest request = GenerateTestCasesRequest.newBuilder()
                    .setCode(code)
                    .setLanguage(language)
                    .setProblemDescription(problemDescription)
                    .build();

            GenerateTestCasesResponse response = blockingStub.generateTestCases(request);

            log.info("gRPC generateTestCases response: success={}", response.getSuccess());
            return response;

        } catch (StatusRuntimeException e) {
            log.error("gRPC generateTestCases failed: {}", e.getStatus(), e);
            return GenerateTestCasesResponse.newBuilder()
                    .setSuccess(false)
                    .setMessage("gRPC 调用失败: " + e.getStatus().getDescription())
                    .build();
        }
    }

    /**
     * 解释代码
     */
    public ExplainCodeResponse explainCode(String code, String language, String detailLevel) {
        try {
            log.info("Calling gRPC explainCode for language: {}, level: {}", language, detailLevel);

            ExplainCodeRequest request = ExplainCodeRequest.newBuilder()
                    .setCode(code)
                    .setLanguage(language)
                    .setDetailLevel(detailLevel)
                    .build();

            ExplainCodeResponse response = blockingStub.explainCode(request);

            log.info("gRPC explainCode response: success={}", response.getSuccess());
            return response;

        } catch (StatusRuntimeException e) {
            log.error("gRPC explainCode failed: {}", e.getStatus(), e);
            return ExplainCodeResponse.newBuilder()
                    .setSuccess(false)
                    .setMessage("gRPC 调用失败: " + e.getStatus().getDescription())
                    .build();
        }
    }
}
