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

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import org.locationtech.jts.geom.Point;

import com.qaup.collision.datacollector.model.enums.CommandReason;
import com.qaup.collision.datacollector.model.enums.CommandType;
import com.qaup.collision.datacollector.model.enums.SignalState;

import java.time.LocalDateTime;

/**
 * 无人车控制指令实体类
 * 
 * 用于存储发送给无人车的控制指令，支持轨迹回放和日志审计
 * 使用PostGIS存储目标位置信息
 */
@Entity
@Table(name = "vehicle_commands", indexes = {
    @Index(name = "idx_vehicle_commands_vehicle_id", columnList = "vehicle_id"),
    @Index(name = "idx_vehicle_commands_timestamp", columnList = "timestamp"),
    @Index(name = "idx_vehicle_commands_trans_id", columnList = "trans_id")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class VehicleCommandEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 消息唯一ID，消息的唯一标识符
     */
    @Column(name = "trans_id", nullable = false, length = 100)
    private String transId;
    
    /**
     * 时间戳
     */
    @Column(name = "timestamp", nullable = false)
    private LocalDateTime timestamp;
    
    /**
     * 车辆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;
    
    /**
     * 指令类型：ALERT、SIGNAL、WARNING、RESUME
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "command_type", nullable = false, length = 20)
    private CommandType commandType;
    
    /**
     * 指令原因：TRAFFIC_LIGHT、AIRCRAFT_CROSSING、SPECIAL_VEHICLE（特勤车辆）、AIRCRAFT_PUSH、RESUME_TRAFFIC
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "command_reason", nullable = false, length = 30)
    private CommandReason commandReason;
    
    /**
     * 信号灯状态（仅当commandType为SIGNAL时有效）
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "signal_state", length = 10)
    private SignalState signalState;
    
    /**
     * 路口ID（仅当commandType为SIGNAL时有效）
     */
    @Column(name = "intersection_id", length = 50)
    private String intersectionId;
    
    /**
     * 目标位置（路口/航空器/特勤车辆位置）- 使用PostGIS POINT类型
     * SRID 4326表示WGS84坐标系统（GPS坐标）
     */
    @Column(name = "target_location", nullable = false, columnDefinition = "geometry(Point,4326)")
    private Point targetLocation;
    
    /**
     * 相对速度（仅当commandType为ALERT/WARNING时有效）
     */
    @Column(name = "relative_speed")
    private Double relativeSpeed;
    
    /**
     * 相对运动X分量（仅当commandType为ALERT/WARNING时有效）
     */
    @Column(name = "relative_motion_x")
    private Double relativeMotionX;
    
    /**
     * 相对运动Y分量（仅当commandType为ALERT/WARNING时有效）
     */
    @Column(name = "relative_motion_y")
    private Double relativeMotionY;
    
    /**
     * 最小距离（仅当commandType为ALERT/WARNING时有效）
     */
    @Column(name = "min_distance")
    private Double minDistance;
    
    /**
     * 创建时间
     */
    @Column(name = "created_at")
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    /**
     * 预设置创建和更新时间
     */
    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
} 