package com.qaup.collision.common.model.spatial;

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

import java.time.LocalDateTime;
import java.time.ZonedDateTime;

/**
 * 机场区域实体类 - 基于PostGIS存储
 * 支持多种几何类型：POLYGON、MULTIPOLYGON等
 * 纯地理功能概念，定义机场内各个区域的地理边界和功能属性
 * 安全控制规则在geofences表中配置
 */
@Entity
@Table(name = "airport_areas", indexes = {
    @Index(name = "idx_airport_area_type", columnList = "type"),
    @Index(name = "idx_airport_area_area_id", columnList = "area_id"),
    @Index(name = "idx_airport_area_enabled", columnList = "enabled"),
    @Index(name = "idx_airport_area_geom", columnList = "boundary"), // PostGIS空间索引
    @Index(name = "idx_airport_areas_functional_category", columnList = "functional_category"),
    @Index(name = "idx_airport_areas_parent_area_id", columnList = "parent_area_id")
})
@Data
@EqualsAndHashCode(callSuper = false)
public class AirportArea {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 区域标识（对应原YAML中的id）
     */
    @Column(name = "area_id", nullable = false, unique = true, length = 50)
    private String areaId;
    
    /**
     * 区域名称
     */
    @Column(name = "name", nullable = false, length = 100)
    private String name;
    
    /**
     * 区域类型：RUNWAY（跑道）、TAXIWAY（滑行道）、APRON（机坪）、
     * TERMINAL（航站楼）、SERVICE_AREA（服务区）、TEMPORARY_CONSTRUCTION（临时施工）等
     */
    @Column(name = "type", nullable = false, length = 30)
    private String type;
    
    /**
     * 区域几何边界 - PostGIS Polygon类型
     * SRID=4326 (WGS84坐标系统)
     */
    @Column(name = "boundary", nullable = false, columnDefinition = "geometry(POLYGON,4326)")
    private Polygon boundary;
    
    /**
     * 区域描述
     */
    @Column(name = "description", length = 500)
    private String description;
    
    /**
     * 生效时间（用于临时区域）
     */
    @Column(name = "active_time")
    private ZonedDateTime activeTime;
    
    /**
     * 失效时间（用于临时区域）
     */
    @Column(name = "expiry_time")
    private ZonedDateTime expiryTime;
    
    /**
     * 是否启用
     */
    @Column(name = "enabled")
    private Boolean enabled = true;
    
    /**
     * 优先级（数值越高优先级越高）
     * 用于重叠区域的规则处理
     */
    @Column(name = "priority")
    private Integer priority = 1;
    
    /**
     * 区域面积（平方米）
     */
    @Column(name = "area_sqm")
    private Double areaSqm;
    
    /**
     * 功能分类（AIRCRAFT_OPERATIONS, PASSENGER_SERVICES, CARGO_OPERATIONS, MAINTENANCE等）
     */
    @Column(name = "functional_category", length = 50)
    private String functionalCategory;
    
    /**
     * 父区域ID（用于区域层次结构）
     */
    @Column(name = "parent_area_id", length = 50)
    private String parentAreaId;
    
    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    /**
     * 数据版本号（用于乐观锁）
     */
    @Version
    @Column(name = "version")
    private Long version;
    
    /**
     * 获取几何体的中心点（用于快速距离计算）
     */
    public org.locationtech.jts.geom.Point getCentroid() {
        return boundary != null ? boundary.getCentroid() : null;
    }
    
    /**
     * 获取区域面积（平方米）
     */
    public Double getArea() {
        return boundary != null ? boundary.getArea() : null;
    }
} 