package com.qaup.collision.websocket.event;

import com.qaup.collision.websocket.message.PathConflictAlertMessage;
import lombok.Builder;
import lombok.Data;

import java.util.UUID;

/**
 * 路径冲突告警WebSocket事件
 * 用于通过WebSocket向前端发送路径冲突告警信息
 * 
 * @author AI Assistant
 * @version 1.0
 * @since 2025-01-17
 */
@Data
@Builder
public class PathConflictAlertWebSocketEvent implements WebSocketEvent {
    
    /**
     * 事件唯一标识
     */
    private final String eventId;

    /**
     * 事件时间戳（微秒级）
     */
    private final long timestamp;

    /**
     * 路径冲突告警消息载荷
     */
    private final PathConflictAlertMessage payload;

    /**
     * 创建路径冲突告警WebSocket事件的工厂方法
     * 
     * @param payload 路径冲突告警消息载荷
     * @return 路径冲突告警WebSocket事件实例
     */
    public static PathConflictAlertWebSocketEvent create(PathConflictAlertMessage payload) {
        return PathConflictAlertWebSocketEvent.builder()
                .eventId(UUID.randomUUID().toString())
                .timestamp(java.time.Instant.now().toEpochMilli() * 1000) // 微秒级绝对时间戳
                .payload(payload)
                .build();
    }
    
    @Override
    public String getEventType() {
        return "PATH_CONFLICT_ALERT"; // 定义事件类型
    }

    @Override
    public Object getPayload() {
        return payload;
    }

    @Override
    public String getEventId() {
        return eventId;
    }

    @Override
    public long getTimestamp() {
        return timestamp;
    }
    
    /**
     * 获取冲突ID
     */
    public Long getConflictId() {
        return getPayload() != null ? ((PathConflictAlertMessage) getPayload()).getConflictId() : null;
    }
    
    /**
     * 获取告警类型
     */
    public String getAlertType() {
        return getPayload() != null && ((PathConflictAlertMessage) getPayload()).getAlertType() != null ?
                ((PathConflictAlertMessage) getPayload()).getAlertType().name() : (String) null;
    }
    
    /**
     * 获取告警级别
     */
    public String getAlertLevel() {
        return getPayload() != null && ((PathConflictAlertMessage) getPayload()).getAlertLevel() != null ?
                ((PathConflictAlertMessage) getPayload()).getAlertLevel().name() : (String) null;
    }
    
    /**
     * 判断是否为紧急告警
     */
    public boolean isEmergencyAlert() {
        return getPayload() != null && ((PathConflictAlertMessage) getPayload()).isEmergencyAlert();
    }
} 