package com.qaup.collision.geofence.model.entity;

import com.qaup.collision.geofence.model.enums.GeofenceEventType;
import com.qaup.collision.rule.model.enums.RuleAlertLevel;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;
import org.locationtech.jts.geom.Point;

import java.time.ZonedDateTime;

/**
 * 电子围栏事件记录实体
 * 记录车辆与围栏的交互事件（进入、离开、违规等）
 */
@Entity
@Table(name = "geofence_events", indexes = {
    @Index(name = "idx_geofence_events_event_id", columnList = "event_id"),
    @Index(name = "idx_geofence_events_vehicle_id", columnList = "vehicle_id"),
    @Index(name = "idx_geofence_events_fence_id", columnList = "fence_id"),
    @Index(name = "idx_geofence_events_time", columnList = "event_time"),
    @Index(name = "idx_geofence_events_type", columnList = "event_type"),
    @Index(name = "idx_geofence_events_alert_level", columnList = "alert_level"),
    @Index(name = "idx_geofence_events_processed", columnList = "processed")
})
@Data
@EqualsAndHashCode(callSuper = false)
public class GeofenceEvent {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 事件唯一标识
     */
    @Column(name = "event_id", nullable = false, unique = true, length = 50)
    private String eventId;
    
    /**
     * 车辆ID（数字主键，关联sys_vehicle_info.vehicle_id）
     */
    @Column(name = "vehicle_id", nullable = false)
    private Long vehicleId;
    
    /**
     * 车牌号（业务标识符）
     */
    @Column(name = "license_plate", nullable = false, length = 50)
    private String licensePlate;
    
    /**
     * 围栏ID
     */
    @Column(name = "fence_id", nullable = false, length = 50)
    private String fenceId;
    
    /**
     * 触发规则ID（可为空）
     */
    @Column(name = "rule_id", length = 50)
    private String ruleId;
    
    /**
     * 事件类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "event_type", nullable = false, length = 20)
    private GeofenceEventType eventType;
    
    /**
     * 事件发生时间
     */
    @Column(name = "event_time", nullable = false)
    private ZonedDateTime eventTime;
    
    /**
     * 事件发生位置
     */
    @Column(name = "event_location", nullable = false, columnDefinition = "geometry(POINT,4326)")
    private Point eventLocation;
    
    /**
     * 车辆速度（m/s）
     */
    @Column(name = "vehicle_speed")
    private Double vehicleSpeed;
    
    /**
     * 车辆航向角（度）
     */
    @Column(name = "vehicle_heading")
    private Double vehicleHeading;
    
    /**
     * 告警级别
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "alert_level", nullable = false, length = 20)
    private RuleAlertLevel alertLevel;
    
    /**
     * 事件消息
     */
    @Column(name = "event_message", length = 500)
    private String eventMessage;
    
    /**
     * 响应动作
     */
    @Column(name = "response_action", length = 30)
    private String responseAction;
    
    /**
     * 响应是否已发送
     */
    @Column(name = "response_sent", nullable = false)
    private Boolean responseSent = false;
    
    /**
     * 响应时间
     */
    @Column(name = "response_time")
    private ZonedDateTime responseTime;
    
    /**
     * 响应结果描述
     */
    @Column(name = "response_result", length = 100)
    private String responseResult;
    
    /**
     * 事件属性（JSON格式存储额外信息）
     */
    @Column(name = "event_attributes", columnDefinition = "jsonb")
    private String eventAttributes;
    
    /**
     * 是否已处理
     */
    @Column(name = "processed", nullable = false)
    private Boolean processed = false;
    
    /**
     * 处理时间
     */
    @Column(name = "processed_at")
    private ZonedDateTime processedAt;
    
    /**
     * 处理人
     */
    @Column(name = "processed_by", length = 100)
    private String processedBy;
    
    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", nullable = false)
    private ZonedDateTime createdAt;
} 