using System.Text.Json.Serialization;

namespace Hc900Crawler.Core.Application.DTOs;

public class ExperionServerConfigDto
{
    public string ServerHostName { get; set; } = "192.168.0.20";
    public int    Port           { get; set; } = 4840;
    public string ClientHostName { get; set; } = "dbsvr";
    public string UserName       { get; set; } = "mngr";
    public string Password       { get; set; } = "mngr";
}

public class ExperionCrawlRequestDto
{
    public ExperionServerConfigDto ServerConfig   { get; set; } = new();
    public List<string>            NodeIds        { get; set; } = new();
    public int                     IntervalSeconds { get; set; } = 1;
    public int                     DurationSeconds { get; set; } = 60;
}

public class ExperionReadTagRequestDto
{
    public ExperionServerConfigDto ServerConfig { get; set; } = new();
    public string                  NodeId       { get; set; } = string.Empty;
}

public class ExperionBrowseRequestDto
{
    public ExperionServerConfigDto ServerConfig  { get; set; } = new();
    public string?                 StartNodeId  { get; set; }
}

public class ExperionCertCreateDto
{
    public string       ClientHostName  { get; set; } = "dbsvr";
    public List<string> SubjectAltNames { get; set; } = new() { "localhost", "192.168.0.50"};
    public string       PfxPassword     { get; set; } = "";
}

public class ExperionCsvImportDto
{
    public string FileName       { get; set; } = string.Empty;
    public string ServerHostName { get; set; } = string.Empty;
    public bool   Truncate       { get; set; } = false;
}

public class ExperionNodeMapCrawlRequestDto
{
    public ExperionServerConfigDto ServerConfig { get; set; } = new();
    public int MaxDepth { get; set; } = 10;
}

public class PointBuilderGroupDto
{
    public List<string> TagPatterns { get; set; } = new();
    public List<string> Attributes  { get; set; } = new();
    public string?      DataType    { get; set; }
}

public class PointBuilderBuildDto
{
    public PointBuilderGroupDto Controller1    { get; set; } = new();
    [JsonPropertyName("analogmon1")]
    public PointBuilderGroupDto AnalogMonitor1 { get; set; } = new();
    public PointBuilderGroupDto Digital1       { get; set; } = new();
    public PointBuilderGroupDto Digital2       { get; set; } = new();
    public PointBuilderGroupDto Custom         { get; set; } = new();
}

public class PointBuilderAddDto
{
    public string NodeId { get; set; } = string.Empty;
}

public class PointBuilderPreviewItem
{
    public string NodeId   { get; set; } = string.Empty;
    public string TagName  { get; set; } = string.Empty;
    public string Name     { get; set; } = string.Empty;
    public string DataType { get; set; } = string.Empty;
    public string Group    { get; set; } = string.Empty;
}

public class PointBuilderPreviewResult
{
    public int                           Count { get; set; }
    public List<PointBuilderPreviewItem> Items { get; set; } = new();
}

public class PointBuilderApplyDto
{
    public List<string> SelectedNodeIds { get; set; } = new();
}

// ── TimeScaleDB 하이퍼테이블 ────────────────────────────────────────────────────

public class HypertableStatusDto
{
    public bool IsHypertable { get; set; }
    public string? TableName { get; set; }
    public string? StatusMessage { get; set; }
    public int RecordCount { get; set; }
    public bool HasRetentionPolicy { get; set; }
    public bool HasCompression { get; set; }
    public bool HasContinuousAggregate { get; set; }
}

// ── History Interval Query ─────────────────────────────────────────────────────

public class HistoryIntervalQueryRequestDto
{
    public List<string> TagNames { get; set; } = new();
    public DateTime? From { get; set; }
    public DateTime? To { get; set; }
    /// <summary>
    /// 조회 간격 (예: 1분, 5분, 1시간). 기본값은 history_table의 저장 간격(60초).
    /// </summary>
    public string Interval { get; set; } = "1 minute";
    public int Limit { get; set; } = 1000;
}

public class HistoryIntervalQueryResponseDto
{
    public List<string> TagNames { get; set; } = new();
    public List<HistoryIntervalRowDto> Rows { get; set; } = new();
    /// <summary>기본 저장 간격 (초)</summary>
    public int BaseIntervalSeconds { get; set; } = 60;
    /// <summary>사용자 지정 조회 간격</summary>
    public string QueryInterval { get; set; } = "1 minute";
}

public class HistoryIntervalRowDto
{
    public DateTime TimeBucket { get; set; }
    public IReadOnlyDictionary<string, string?> Values { get; set; } = new Dictionary<string, string?>();
}

public class HypertableCreateDto
{
    public string TableName { get; set; } = "history_table";
    public string TimeColumn { get; set; } = "recorded_at";
    public string TimeInterval { get; set; } = "1 day";
    public bool MigrateData { get; set; } = true;
    public bool SetRetentionPolicy { get; set; } = true;
    public string RetentionPeriod { get; set; } = "90 days";
    public bool EnableCompression { get; set; } = true;
    public string CompressionPeriod { get; set; } = "1 day";
    public bool CreateContinuousAggregate { get; set; } = true;
}

// ── OPC UA Write Result DTOs ──────────────────────────────────────────────────

public class ExperionWriteResultDto
{
    public bool Success { get; set; }
    public string NodeId { get; set; } = "";
    public string Status { get; set; } = "";
    public string? Error { get; set; }
}

public class ExperionModeResultDto
{
    public bool Success { get; set; }
    public string NodeId { get; set; } = "";
    public string Mode { get; set; } = "";
    public int EnumValue { get; set; }
    public string Status { get; set; } = "";
    public string? Error { get; set; }
}
