package com.qaup.collision.rule.service;

import com.qaup.collision.rule.event.RuleViolationEvent;
import com.qaup.collision.rule.model.entity.SpatialRule;
import com.qaup.collision.rule.model.enums.RuleExecutionResult;
import com.qaup.collision.common.model.spatial.VehicleLocation;
import org.locationtech.jts.geom.Point;

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

/**
 * 规则违规事件处理器接口
 * 负责处理规则违规事件的生成、处理、通知和响应
 * 
 * @author AI Assistant
 * @since 2025-01-17
 */
public interface RuleViolationProcessor {

    // ============================================
    // 违规事件生成
    // ============================================

    /**
     * 创建违规事件
     * 
     * @param rule 违规的规则
     * @param vehicleLocation 车辆位置信息
     * @param executionResult 规则执行结果
     * @param violationDetails 违规详情
     * @return 创建的违规事件
     */
    RuleViolationEvent createViolationEvent(
        SpatialRule rule,
        VehicleLocation vehicleLocation,
        RuleExecutionResult executionResult,
        Map<String, Object> violationDetails
    );

    /**
     * 批量创建违规事件
     * 
     * @param violations 违规信息列表
     * @return 创建的违规事件列表
     */
    List<RuleViolationEvent> createViolationEvents(List<ViolationInfo> violations);

    // ============================================
    // 违规事件处理
    // ============================================

    /**
     * 处理违规事件
     * 包括事件验证、严重程度评估、响应动作执行等
     * 
     * @param violationEvent 违规事件
     * @return 处理结果
     */
    ViolationProcessingResult processViolationEvent(RuleViolationEvent violationEvent);

    /**
     * 批量处理违规事件
     * 
     * @param violationEvents 违规事件列表
     * @return 处理结果列表
     */
    List<ViolationProcessingResult> processViolationEvents(List<RuleViolationEvent> violationEvents);

    // 异步处理方法已移除 - 简化架构，统一使用同步处理

    // ============================================
    // 违规事件状态管理
    // ============================================

    /**
     * 确认违规事件
     * 
     * @param eventId 事件ID
     * @param acknowledgedBy 确认人
     * @param notes 确认备注
     * @return 是否成功
     */
    boolean acknowledgeViolation(String eventId, String acknowledgedBy, String notes);

    /**
     * 解决违规事件
     * 
     * @param eventId 事件ID
     * @param resolvedBy 解决人
     * @param resolution 解决方案
     * @return 是否成功
     */
    boolean resolveViolation(String eventId, String resolvedBy, String resolution);

    /**
     * 忽略违规事件
     * 
     * @param eventId 事件ID
     * @param ignoredBy 忽略人
     * @param reason 忽略原因
     * @return 是否成功
     */
    boolean ignoreViolation(String eventId, String ignoredBy, String reason);

    // ============================================
    // 违规事件查询
    // ============================================

    /**
     * 查询待处理的违规事件
     * 
     * @param limit 限制数量
     * @return 待处理事件列表
     */
    List<RuleViolationEvent> getPendingViolations(int limit);

    /**
     * 查询高严重程度的违规事件
     * 
     * @param hours 过去几小时内
     * @return 高严重程度事件列表
     */
    List<RuleViolationEvent> getHighSeverityViolations(int hours);

    /**
     * 查询车辆的违规历史
     * 
     * @param vehicleId 车辆ID
     * @param days 过去几天内
     * @return 违规历史列表
     */
    List<RuleViolationEvent> getVehicleViolationHistory(String vehicleId, int days);

    // ============================================
    // 违规统计分析
    // ============================================

    /**
     * 获取违规统计信息
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计信息
     */
    ViolationStatistics getViolationStatistics(LocalDateTime startTime, LocalDateTime endTime);

    /**
     * 获取规则违规频率统计
     * 
     * @param ruleId 规则ID
     * @param days 过去几天内
     * @return 违规频率统计
     */
    RuleViolationFrequency getRuleViolationFrequency(String ruleId, int days);

    /**
     * 获取热点违规区域
     * 
     * @param days 过去几天内
     * @param limit 限制数量
     * @return 热点区域列表
     */
    List<ViolationHotspot> getViolationHotspots(int days, int limit);

    // ============================================
    // 内部类定义
    // ============================================

    /**
     * 违规信息
     */
    class ViolationInfo {
        private SpatialRule rule;
        private VehicleLocation vehicleLocation;
        private RuleExecutionResult executionResult;
        private Map<String, Object> violationDetails;

        // 构造函数
        public ViolationInfo(SpatialRule rule, VehicleLocation vehicleLocation, 
                           RuleExecutionResult executionResult, Map<String, Object> violationDetails) {
            this.rule = rule;
            this.vehicleLocation = vehicleLocation;
            this.executionResult = executionResult;
            this.violationDetails = violationDetails;
        }

        // Getters
        public SpatialRule getRule() { return rule; }
        public VehicleLocation getVehicleLocation() { return vehicleLocation; }
        public RuleExecutionResult getExecutionResult() { return executionResult; }
        public Map<String, Object> getViolationDetails() { return violationDetails; }
    }

    /**
     * 违规处理结果
     */
    class ViolationProcessingResult {
        private String eventId;
        private boolean success;
        private String message;
        private List<String> actionsExecuted;
        private Exception error;

        // 构造函数
        public ViolationProcessingResult(String eventId, boolean success, String message) {
            this.eventId = eventId;
            this.success = success;
            this.message = message;
        }

        // Getters and Setters
        public String getEventId() { return eventId; }
        public boolean isSuccess() { return success; }
        public String getMessage() { return message; }
        public List<String> getActionsExecuted() { return actionsExecuted; }
        public void setActionsExecuted(List<String> actionsExecuted) { this.actionsExecuted = actionsExecuted; }
        public Exception getError() { return error; }
        public void setError(Exception error) { this.error = error; }
    }

    /**
     * 违规统计信息
     */
    class ViolationStatistics {
        private long totalViolations;
        private long pendingViolations;
        private long resolvedViolations;
        private Map<String, Long> violationsByType;
        private Map<String, Long> violationsBySeverity;
        private Map<String, Long> violationsByRule;

        // Getters and Setters
        public long getTotalViolations() { return totalViolations; }
        public void setTotalViolations(long totalViolations) { this.totalViolations = totalViolations; }
        public long getPendingViolations() { return pendingViolations; }
        public void setPendingViolations(long pendingViolations) { this.pendingViolations = pendingViolations; }
        public long getResolvedViolations() { return resolvedViolations; }
        public void setResolvedViolations(long resolvedViolations) { this.resolvedViolations = resolvedViolations; }
        public Map<String, Long> getViolationsByType() { return violationsByType; }
        public void setViolationsByType(Map<String, Long> violationsByType) { this.violationsByType = violationsByType; }
        public Map<String, Long> getViolationsBySeverity() { return violationsBySeverity; }
        public void setViolationsBySeverity(Map<String, Long> violationsBySeverity) { this.violationsBySeverity = violationsBySeverity; }
        public Map<String, Long> getViolationsByRule() { return violationsByRule; }
        public void setViolationsByRule(Map<String, Long> violationsByRule) { this.violationsByRule = violationsByRule; }
    }

    /**
     * 规则违规频率统计
     */
    class RuleViolationFrequency {
        private String ruleId;
        private String ruleName;
        private long totalViolations;
        private double averageViolationsPerDay;
        private Map<String, Long> violationsByDay;

        // Getters and Setters
        public String getRuleId() { return ruleId; }
        public void setRuleId(String ruleId) { this.ruleId = ruleId; }
        public String getRuleName() { return ruleName; }
        public void setRuleName(String ruleName) { this.ruleName = ruleName; }
        public long getTotalViolations() { return totalViolations; }
        public void setTotalViolations(long totalViolations) { this.totalViolations = totalViolations; }
        public double getAverageViolationsPerDay() { return averageViolationsPerDay; }
        public void setAverageViolationsPerDay(double averageViolationsPerDay) { this.averageViolationsPerDay = averageViolationsPerDay; }
        public Map<String, Long> getViolationsByDay() { return violationsByDay; }
        public void setViolationsByDay(Map<String, Long> violationsByDay) { this.violationsByDay = violationsByDay; }
    }

    /**
     * 违规热点区域
     */
    class ViolationHotspot {
        private String areaId;
        private String areaName;
        private Point centerLocation;
        private long violationCount;
        private double violationDensity;

        // Getters and Setters
        public String getAreaId() { return areaId; }
        public void setAreaId(String areaId) { this.areaId = areaId; }
        public String getAreaName() { return areaName; }
        public void setAreaName(String areaName) { this.areaName = areaName; }
        public Point getCenterLocation() { return centerLocation; }
        public void setCenterLocation(Point centerLocation) { this.centerLocation = centerLocation; }
        public long getViolationCount() { return violationCount; }
        public void setViolationCount(long violationCount) { this.violationCount = violationCount; }
        public double getViolationDensity() { return violationDensity; }
        public void setViolationDensity(double violationDensity) { this.violationDensity = violationDensity; }
    }
} 