package com.usky.cdi.controller;

import com.usky.cdi.service.impl.AlarmDataTransferService;
import com.usky.cdi.service.vo.alarm.AlarmMessageVO;
import com.usky.common.core.bean.ApiResult;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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;

/**
 * 告警数据 HTTP 入口：将告警上报至市适配平台（MQTT）。
 *
 * @author han
 * @date 2025/12/08
 */
@RestController
@RequestMapping("/api/alarm")
@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
@RequiredArgsConstructor
public class AlarmDataController {

    private final AlarmDataTransferService alarmDataTransferService;

    private static ApiResult<String> toSubmitResult(boolean success) {
        return success ? ApiResult.success("上报成功") : ApiResult.error("上报失败");
    }

    /**
     * 告警推送
     */
    @PostMapping("/alarmMessage")
    public ApiResult<String> alarmMessage(@RequestBody AlarmMessageVO<?> vo) {
        return toSubmitResult(alarmDataTransferService.publishAlarm(vo));
    }

}
