package com.xindazhou.vehicle.controller.admin.vehicle;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.vehicle.service.impl.VehicleControlServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 车辆报警 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 车辆报警")
@RestController
@RequestMapping("/alarm")
@Validated
public class VehicleAlarmController {

    @Resource
    private VehicleControlServiceImpl vehicleControlService;

    /**
     * 推送报警通知（测试用）
     * 
     * 流程：管理后台 → 业务服务 → 车辆服务 → Redis MQ → 消息服务 → SocketIO → 客户端
     * 
     * 注意：管理后台通过业务服务调用车辆服务，避免直接依赖，防止循环依赖
     */
    @PostMapping("/push")
    @Operation(summary = "推送报警通知（测试用）", 
               description = "测试报警推送功能。流程：管理后台 → 业务服务 → 车辆服务 → Redis MQ(后期换kafka) → 消息服务 → SocketIO → 客户端")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1")
    @Parameter(name = "vehicleId", description = "车辆ID", required = true, example = "1")
    @Parameter(name = "alarmType", description = "报警类型", required = false, example = "test_alarm")
    public CommonResult<Boolean> pushAlarm(@RequestParam("userId") Long userId,
                                           @RequestParam("vehicleId") Long vehicleId,
                                           @RequestParam(value = "alarmType", required = false, defaultValue = "test_alarm") String alarmType,
                                           @RequestBody(required = false) Map<String, Object> alarmData) {
        // 构建报警消息数据
        Map<String, Object> data = new HashMap<>();
        if (alarmData != null) {
            data.putAll(alarmData);
        }
        data.putIfAbsent("message", "测试报警消息");
        data.putIfAbsent("timestamp", System.currentTimeMillis());
        
        // 调用车辆服务发送报警消息到 Redis MQ
        vehicleControlService.sendAlarmMessage(userId, vehicleId, alarmType, data);
        return success(true);
    }

}

