package com.qaup.collision.websocket.message;

import com.qaup.collision.geofence.model.dto.GeofenceAccessCheckResult;
import com.qaup.collision.rule.model.enums.RuleAlertLevel;

import lombok.Data;
import lombok.Builder;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 电子围栏告警WebSocket消息载荷
 * 
 * @author qaup-collision
 * @date 2025-01-15
 */
@Data
@Builder
public class GeofenceAlertPayload {
    
    /**
     * 告警唯一标识
     */
    private String alertId;
    
    /**
     * 无人车ID
     */
    private String vehicleId;
    
    /**
     * 无人车名称/车牌号
     */
    private String vehicleName;
    
    /**
     * 区域编码
     */
    private String areaCode;
    
    /**
     * 区域名称
     */
    private String areaName;
    
    /**
     * 告警类型
     */
    private GeofenceAccessCheckResult.AccessResultType alertType;
    
    /**
     * 告警级别
     */
    private RuleAlertLevel alertLevel;
    
    /**
     * 告警消息
     */
    private String message;
    
    /**
     * 详细原因
     */
    private String reason;
    
    /**
     * 无人车当前位置
     */
    private Position position;
    
    /**
     * 当前速度 (km/h)
     */
    private Double currentSpeed;
    
    /**
     * 触发的规则列表
     */
    private List<TriggeredRule> triggeredRules;
    
    /**
     * 建议操作
     */
    private List<String> recommendedActions;
    
    /**
     * 告警时间
     */
    private LocalDateTime alertTime;
    
    /**
     * 位置信息
     */
    @Data
    @Builder
    public static class Position {
        /**
         * 纬度
         */
        private Double latitude;
        
        /**
         * 经度
         */
        private Double longitude;
        
        /**
         * 高度（米）
         */
        private Double altitude;
    }
    
    /**
     * 触发的规则信息
     */
    @Data
    @Builder
    public static class TriggeredRule {
        /**
         * 规则ID
         */
        private String ruleId;
        
        /**
         * 规则名称
         */
        private String ruleName;
        
        /**
         * 规则分类
         */
        private String ruleCategory;
        
        /**
         * 违规类型
         */
        private String violationType;
        
        /**
         * 告警级别
         */
        private RuleAlertLevel alertLevel;
        
        /**
         * 规则消息
         */
        private String message;
    }
    
    /**
     * 从GeofenceAccessCheckResult创建载荷
     */
    public static GeofenceAlertPayload fromGeofenceResult(
            GeofenceAccessCheckResult result, 
            String vehicleName,
            Double latitude, 
            Double longitude, 
            Double altitude,
            Double currentSpeed) {
        
        GeofenceAlertPayloadBuilder builder = GeofenceAlertPayload.builder()
                .alertId(java.util.UUID.randomUUID().toString())
                .vehicleId(result.getVehicleId())
                .vehicleName(vehicleName)
                .areaCode(result.getAreaCode())
                .areaName(result.getAreaName())
                .alertType(result.getResultType())
                .alertLevel(result.getAlertLevel())
                .message(result.getMessage())
                .reason(result.getReason())
                .currentSpeed(currentSpeed)
                .alertTime(result.getCheckTime() != null ? result.getCheckTime() : LocalDateTime.now())
                .recommendedActions(result.getRecommendedActions());
        
        // 设置位置信息
        if (latitude != null && longitude != null) {
            builder.position(Position.builder()
                    .latitude(latitude)
                    .longitude(longitude)
                    .altitude(altitude)
                    .build());
        }
        
        // 转换触发的规则
        if (result.getTriggeredRules() != null && !result.getTriggeredRules().isEmpty()) {
            List<TriggeredRule> rules = result.getTriggeredRules().stream()
                    .map(rule -> TriggeredRule.builder()
                            .ruleId(rule.getRuleId())
                            .ruleName(rule.getRuleName())
                            .ruleCategory(rule.getRuleCategory())
                            .violationType(rule.getViolationType())
                            .alertLevel(rule.getAlertLevel())
                            .message(rule.getMessage())
                            .build())
                    .toList();
            builder.triggeredRules(rules);
        }
        
        return builder.build();
    }
}