using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Hc900Crawler.Core.Domain.Entities;

/// <summary>P&ID 도면에서 추출한 장비/계기 정보</summary>
[Table("pid_equipment")]
public class PidEquipment
{
    [Column("id")]
    public long Id { get; set; }

    [Required]
    [MaxLength(50)]
    [Column("tag_no")]
    public string TagNo { get; set; } = string.Empty;

    [MaxLength(200)]
    [Column("equipment_name")]
    public string? EquipmentName { get; set; }

    [MaxLength(10)]
    [Column("instrument_type")]
    public string? InstrumentType { get; set; }

    [MaxLength(100)]
    [Column("line_number")]
    public string? LineNumber { get; set; }

    [MaxLength(50)]
    [Column("pid_drawing_no")]
    public string? PidDrawingNo { get; set; }

    [Column("confidence")]
    public double Confidence { get; set; }

    [Column("is_active")]
    public bool IsActive { get; set; } = true;

    [Column("extracted_at")]
    public DateTime ExtractedAt { get; set; } = DateTime.UtcNow;

    [Column("updated_at")]
    public DateTime? UpdatedAt { get; set; }

    // 외래 키 - 기존 RealtimePoint.Id는 int 타입
    [Column("experion_tag_id")]
    public int? ExperionTagId { get; set; }

    // FK 네비게이션 프로퍼티
    public RealtimePoint? ExperionTag { get; set; }

    // ── 카테고리 분류 ──
    public const string CategoryInstrument       = "instrument";
    public const string CategoryPowerEquipment   = "power_equipment";
    public const string CategoryStorageEquipment = "storage_equipment";
    public const string CategoryProcessEquipment = "process_equipment";
    public const string CategoryUtilityEquipment = "utility_equipment";
    public const string CategoryPipings          = "pipings";

    public static readonly string[] AllCategories =
        [CategoryInstrument, CategoryPowerEquipment, CategoryStorageEquipment,
         CategoryProcessEquipment, CategoryUtilityEquipment, CategoryPipings];

    [MaxLength(30)]
    [Column("category")]
    public string? Category { get; set; }

    // ── 계기 하위 분류 (B안: instrument 내 현장 vs 시스템) ──
    // field  = 현장 실물 계기 (PG/TG/PSV/1차 측정요소·전송기 등 물리 기기)
    // system = DCS/SCADA/PLC DB 포인트 (지시·제어·알람·적산 함수 블록)
    // instrument 카테고리에만 의미, 그 외는 null
    public const string TagClassField  = "field";
    public const string TagClassSystem = "system";

    [MaxLength(10)]
    [Column("tag_class")]
    public string? TagClass { get; set; }

    /// <summary>prefix rule 기반 DCS 함수블록 여부: TRUE=FIC/TIC류 DCS 블록, FALSE=FT/FCV류 현장 계기</summary>
    [Column("tag_dcs")]
    public bool TagDcs { get; set; } = false;

    [MaxLength(100)]
    [Column("role")]
    public string? Role { get; set; }

    [MaxLength(50)]
    [Column("from_tag")]
    public string? FromTag { get; set; }

    [MaxLength(50)]
    [Column("to_tag")]
    public string? ToTag { get; set; }

    [MaxLength(255)]
    [Column("from_at")]
    public string? FromAt { get; set; }

    [MaxLength(255)]
    [Column("to_at")]
    public string? ToAt { get; set; }

    // 엑셀 import 로 사람이 교정한 행 표시.
    // true 이면 AnalyzeConnectionsAsync 가 from_tag/to_tag 를 초기화·재계산하지 않음.
    [Column("connection_locked")]
    public bool ConnectionLocked { get; set; }

    // ── DXF 좌표 ──
    [Column("pos_x")]
    public double? PosX { get; set; }

    [Column("pos_y")]
    public double? PosY { get; set; }

    [MaxLength(255)]
    [Column("drawing_file")]
    public string? DrawingFile { get; set; }

    // ── UI 전용 (DB 컬럼 없음, API 응답 시 tag_metadata LEFT JOIN) ──
    [System.ComponentModel.DataAnnotations.Schema.NotMapped]
    public string? SubArea { get; set; }
}
