namespace OpcUaManager.Models;

// ── 인증서 관련 ────────────────────────────────────────────────

/// <summary>프론트엔드에서 전달받는 인증서 생성 요청</summary>
public class CertCreateRequest
{
    /// <summary>내 컴퓨터(클라이언트) 호스트명 e.g. "dbsvr"</summary>
    public string ClientHostName { get; set; } = string.Empty;

    /// <summary>OPC Application 이름 e.g. "OpcTestClient"</summary>
    public string ApplicationName { get; set; } = string.Empty;

    /// <summary>상대 OPC 서버 호스트명 (SAN DNS) e.g. "opc-server-01"</summary>
    public string ServerHostName { get; set; } = string.Empty;

    /// <summary>상대 OPC 서버 IP (SAN IP) e.g. "192.168.0.20"</summary>
    public string ServerIp { get; set; } = string.Empty;

    /// <summary>PFX 비밀번호 (없으면 빈 문자열)</summary>
    public string PfxPassword { get; set; } = string.Empty;

    /// <summary>인증서 유효 기간(일)</summary>
    public int ValidDays { get; set; } = 365;
}

/// <summary>인증서 생성 결과</summary>
public class CertCreateResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;

    /// <summary>생성된 PFX 파일의 서버 내 절대 경로</summary>
    public string PfxPath { get; set; } = string.Empty;

    /// <summary>적용된 Application URI</summary>
    public string ApplicationUri { get; set; } = string.Empty;

    /// <summary>썸프린트(SHA-1 hex)</summary>
    public string Thumbprint { get; set; } = string.Empty;

    public string SerialNumber { get; set; } = string.Empty;
    public string NotBefore { get; set; } = string.Empty;
    public string NotAfter { get; set; } = string.Empty;
}

// ── OPC 세션 관련 ────────────────────────────────────────────

public class ConnectRequest
{
    public string ServerIp { get; set; } = string.Empty;
    public int Port { get; set; } = 4840;
    public string UserName { get; set; } = string.Empty;
    public string Password { get; set; } = string.Empty;
    public string SecurityPolicy { get; set; } = "Basic256Sha256";
    public int SessionTimeoutMs { get; set; } = 60000;

    /// <summary>사용할 PFX 경로 (cert 생성 후 전달)</summary>
    public string PfxPath { get; set; } = string.Empty;
    public string PfxPassword { get; set; } = string.Empty;
    public string ApplicationName { get; set; } = "OpcTestClient";
    public string ApplicationUri { get; set; } = string.Empty;
}

public class ConnectResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public string SessionId { get; set; } = string.Empty;
    public string EndpointUrl { get; set; } = string.Empty;
    public string SecurityMode { get; set; } = string.Empty;
}

// ── Crawler 관련 ─────────────────────────────────────────────

public class CrawlRequest
{
    public string StartNodeId { get; set; } = "ns=1;s=$assetmodel";
    public int MaxDepth { get; set; } = 5;
}

public class TagMaster
{
    public string TagName { get; set; } = string.Empty;
    public string FullNodeId { get; set; } = string.Empty;
    public string NodeClass { get; set; } = string.Empty;
    public string DataType { get; set; } = string.Empty;
    public int Level { get; set; }
}

public class CrawlResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public int TotalNodes { get; set; }
    public List<TagMaster> Tags { get; set; } = [];
    public string CsvPath { get; set; } = string.Empty;
}

// ── Database 관련 ─────────────────────────────────────────────

public class DbWriteRequest
{
    /// <summary>읽을 OPC 태그 Node ID</summary>
    public string TagNodeId { get; set; } = string.Empty;

    /// <summary>DB에 저장할 태그 이름</summary>
    public string TagName { get; set; } = string.Empty;

    /// <summary>반복 횟수</summary>
    public int Count { get; set; } = 5;

    /// <summary>읽기 간격 (ms)</summary>
    public int IntervalMs { get; set; } = 2000;

    // DB 접속 정보
    public string DbHost { get; set; } = "localhost";
    public string DbName { get; set; } = "opcdb";
    public string DbUser { get; set; } = "postgres";
    public string DbPassword { get; set; } = "postgres";
}

public class DbWriteRecord
{
    public int Seq { get; set; }
    public DateTime Timestamp { get; set; }
    public string TagName { get; set; } = string.Empty;
    public double Value { get; set; }
    public string StatusCode { get; set; } = string.Empty;
    public bool DbSaved { get; set; }
}

public class DbWriteResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public int SavedCount { get; set; }
    public List<DbWriteRecord> Records { get; set; } = [];
}

public class DbQueryResult
{
    public bool Success { get; set; }
    public string Message { get; set; } = string.Empty;
    public int TotalCount { get; set; }
    public List<OpcHistoryRow> Rows { get; set; } = [];
}

public class OpcHistoryRow
{
    public long Id { get; set; }
    public string TagName { get; set; } = string.Empty;
    public double TagValue { get; set; }
    public string StatusCode { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
}

// ── 공통 ─────────────────────────────────────────────────────

public class StatusCodeInfo
{
    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;
}

public class ApiError
{
    public string Error { get; set; } = string.Empty;
    public string Detail { get; set; } = string.Empty;
}
