using System.ComponentModel.DataAnnotations.Schema;

namespace ExperionCrawler.Core.Domain.Entities;

/// <summary>OPC UA 노드 태그 읽기 결과 (메모리)</summary>
public class ExperionTag
{
    public int    Id          { get; set; }
    public string NodeId      { get; set; } = string.Empty;
    public string DisplayName { get; set; } = string.Empty;
    public string? Value      { get; set; }
    public string? DataType   { get; set; }
    public string StatusCode  { get; set; } = "Good";
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;
    public bool IsGood => StatusCode == "Good";
}

/// <summary>크롤링으로 수집한 레코드 (DB/CSV 저장 단위)</summary>
public class ExperionRecord
{
    public int    Id          { get; set; }
    public string NodeId      { get; set; } = string.Empty;
    public string? Value      { get; set; }
    public string StatusCode  { get; set; } = "Good";
    public DateTime CollectedAt { get; set; } = DateTime.UtcNow;
    public string? SessionId  { get; set; }
}

/// <summary>Experion 서버 접속 설정</summary>
public class ExperionServerConfig
{
    public int    Id               { get; set; }
    public string ServerHostName   { get; set; } = string.Empty;
    public int    Port             { get; set; } = 4840;
    public string ClientHostName   { get; set; } = string.Empty;
    public string UserName         { get; set; } = string.Empty;
    public string Password         { get; set; } = string.Empty;

    public string EndpointUrl    => $"opc.tcp://{ServerHostName}:{Port}";
    public string ApplicationUri => $"urn:{ClientHostName}:ExperionCrawlerClient";
}

/// <summary>OPC UA 전체 노드맵 원시 데이터 (AssetLoader binary COPY 대상)</summary>
[Table("raw_node_map")]
public class RawNodeMap
{
    [Column("id")]       public int    Id       { get; set; }
    [Column("level")]    public int    Level    { get; set; }
    [Column("class")]    public string Class    { get; set; } = string.Empty;
    [Column("name")]     public string Name     { get; set; } = string.Empty;
    [Column("node_id")]  public string NodeId   { get; set; } = string.Empty;
    [Column("data_type")]public string DataType { get; set; } = string.Empty;
}

/// <summary>raw_node_map 에서 빌드된 master 테이블</summary>
[Table("node_map_master")]
public class NodeMapMaster
{
    [Column("id")]       public int    Id       { get; set; }
    [Column("level")]    public int    Level    { get; set; }
    [Column("class")]    public string Class    { get; set; } = string.Empty;
    [Column("name")]     public string Name     { get; set; } = string.Empty;
    [Column("node_id")]  public string NodeId   { get; set; } = string.Empty;
    [Column("data_type")]public string DataType { get; set; } = string.Empty;
}

/// <summary>realtime_table — 실시간 모니터링 포인트</summary>
[Table("realtime_table")]
public class RealtimePoint
{
    [Column("id")]        public int     Id        { get; set; }
    [Column("tagname")]   public string  TagName   { get; set; } = string.Empty;
    [Column("node_id")]   public string  NodeId    { get; set; } = string.Empty;
    [Column("livevalue")] public string? LiveValue { get; set; }
    [Column("timestamp")] public DateTime Timestamp { get; set; } = DateTime.UtcNow;
}

/// <summary>history_table — 시계열 이력 스냅샷</summary>
[Table("history_table")]
public class HistoryRecord
{
    [Column("id")]          public int     Id          { get; set; }
    [Column("tagname")]     public string  TagName     { get; set; } = string.Empty;
    [Column("node_id")]     public string  NodeId      { get; set; } = string.Empty;
    [Column("value")]       public string? Value       { get; set; }
    [Column("recorded_at")] public DateTime RecordedAt { get; set; } = DateTime.UtcNow;
}

/// <summary>statuscode.json 항목</summary>
public class ExperionStatusCodeInfo
{
    public string Name        { get; set; } = string.Empty;
    public string Hex         { get; set; } = string.Empty;
    public ulong  Decimal     { get; set; }
    public string Description { get; set; } = string.Empty;
}
