package com.xindazhou.business.controller.admin.test;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.message.api.MessageSendApi;
import com.xindazhou.message.api.dto.send.MessageSendReqDTO;
import com.xindazhou.vehicle.api.VehicleApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

/**
 * 服务间调用测试 Controller
 * 用于验证业务服务、车辆服务、消息服务之间的 Feign 调用是否正常
 *
 * @author xindazhou
 */
@Slf4j
@Tag(name = "管理后台 - 服务调用测试")
@RestController
@RequestMapping("/admin-api/test/service-call")
public class ServiceCallTestController {

    @Resource
    private VehicleApi vehicleApi;

    @Resource
    private MessageSendApi messageSendApi;

    @GetMapping("/vehicle")
    @Operation(summary = "测试调用车辆服务", description = "业务服务 -> 车辆服务")
    public CommonResult<String> testVehicleService(@RequestParam(value = "id", required = false, defaultValue = "1") Long id) {
        log.info("[ServiceCallTest] 开始测试调用车辆服务，车辆ID: {}", id);
        try {
            CommonResult<?> result = vehicleApi.getVehicle(id);
            log.info("[ServiceCallTest] 车辆服务调用成功，结果: {}", result);
            if (result.isSuccess()) {
                return CommonResult.success("车辆服务调用成功！结果: " + result.getData());
            } else {
                return CommonResult.error(result.getCode(), "车辆服务调用失败: " + result.getMsg());
            }
        } catch (Exception e) {
            log.error("[ServiceCallTest] 车辆服务调用异常", e);
            return CommonResult.error(500, "车辆服务调用异常: " + e.getMessage());
        }
    }

    @GetMapping("/message")
    @Operation(summary = "测试调用消息服务", description = "业务服务 -> 消息服务")
    public CommonResult<String> testMessageService(@RequestParam(value = "userId", required = false, defaultValue = "1") Long userId) {
        log.info("[ServiceCallTest] 开始测试调用消息服务，用户ID: {}", userId);
        try {
            MessageSendReqDTO reqDTO = new MessageSendReqDTO();
            reqDTO.setUserId(userId);
            reqDTO.setUserType(1); // C端用户
            reqDTO.setMessageType("NOTIFY"); // 站内信
            reqDTO.setTemplateCode("test_template");
            // templateParams 是 Map<String, Object> 类型
            reqDTO.setTemplateParams(java.util.Map.of("name", "测试用户"));
            
            CommonResult<Long> result = messageSendApi.sendMessage(reqDTO);
            log.info("[ServiceCallTest] 消息服务调用成功，结果: {}", result);
            if (result.isSuccess()) {
                return CommonResult.success("消息服务调用成功！消息ID: " + result.getData());
            } else {
                return CommonResult.error(result.getCode(), "消息服务调用失败: " + result.getMsg());
            }
        } catch (Exception e) {
            log.error("[ServiceCallTest] 消息服务调用异常", e);
            return CommonResult.error(500, "消息服务调用异常: " + e.getMessage());
        }
    }

    @GetMapping("/all")
    @Operation(summary = "测试所有服务调用", description = "业务服务 -> 车辆服务 + 消息服务")
    public CommonResult<String> testAllServices() {
        log.info("[ServiceCallTest] 开始测试所有服务调用");
        StringBuilder result = new StringBuilder();
        
        // 测试车辆服务
        try {
            CommonResult<?> vehicleResult = vehicleApi.getVehicle(1L);
            if (vehicleResult.isSuccess()) {
                result.append("✅ 车辆服务调用成功\n");
            } else {
                result.append("❌ 车辆服务调用失败: ").append(vehicleResult.getMsg()).append("\n");
            }
        } catch (Exception e) {
            result.append("❌ 车辆服务调用异常: ").append(e.getMessage()).append("\n");
        }
        
        // 测试消息服务
        try {
            MessageSendReqDTO reqDTO = new MessageSendReqDTO();
            reqDTO.setUserId(1L);
            reqDTO.setUserType(1);
            reqDTO.setMessageType("NOTIFY"); // 站内信
            reqDTO.setTemplateCode("test_template");
            // templateParams 是 Map<String, Object> 类型
            reqDTO.setTemplateParams(java.util.Map.of("name", "测试用户"));
            
            CommonResult<Long> messageResult = messageSendApi.sendMessage(reqDTO);
            if (messageResult.isSuccess()) {
                result.append("✅ 消息服务调用成功\n");
            } else {
                result.append("❌ 消息服务调用失败: ").append(messageResult.getMsg()).append("\n");
            }
        } catch (Exception e) {
            result.append("❌ 消息服务调用异常: ").append(e.getMessage()).append("\n");
        }
        
        return CommonResult.success(result.toString());
    }
}

