using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using Opc.Ua;
using Opc.Ua.Client;
using Npgsql; // 👈 PostgreSQL 라이브러리 추가

namespace OpcConnectionTest
{
    public class StatusCodeInfo
    {
        public string Name { get; set; } = "";
        public string Hex { get; set; } = "";
        public ulong Decimal { get; set; }
        public string Description { get; set; } = "";
    }

    class Program
    {
        static Dictionary<string, StatusCodeInfo> _statusCodeMap = new(StringComparer.OrdinalIgnoreCase);
        
        // 1. DB 연결 문자열 (비밀번호를 본인 설정에 맞게 수정하세요)
        static string dbConnString = "Host=localhost;Username=postgres;Password=postgres;Database=opcdb";

        static void LoadStatusCodes()
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "statuscode.json");
            if (File.Exists(path))
            {
                try {
                    var json = File.ReadAllText(path);
                    var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
                    var list = JsonSerializer.Deserialize<List<StatusCodeInfo>>(json, options);
                    if (list != null) {
                        foreach (var item in list) _statusCodeMap[item.Hex] = item;
                        Console.WriteLine($"✅ {_statusCodeMap.Count}개의 에러 코드 정의 로드 완료.");
                    }
                } catch { }
            }
        }

        // 2. DB 저장 함수
        static async Task SaveToDatabase(string tagName, double val, string status)
        {
            try {
                using var conn = new NpgsqlConnection(dbConnString);
                await conn.OpenAsync();
                
                string sql = "INSERT INTO opc_history (tag_name, tag_value, status_code) VALUES (@tag, @val, @status)";
                using var cmd = new NpgsqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("tag", tagName);
                cmd.Parameters.AddWithValue("val", val);
                cmd.Parameters.AddWithValue("status", status);
                
                await cmd.ExecuteNonQueryAsync();
                Console.WriteLine("💾 DB 저장 완료.");
            }
            catch (Exception ex) {
                Console.WriteLine($"❌ DB 저장 실패: {ex.Message}");
            }
        }

        static async Task Main(string[] args)
        {
            LoadStatusCodes();

            string serverHostName = "192.168.0.20";
            string clientHostName = "dbsvr"; 
            string endpointUrl = $"opc.tcp://{serverHostName}:4840";
            string applicationUri = $"urn:{clientHostName}:OpcTestClient"; 
            string pfxPath = Path.Combine(Directory.GetCurrentDirectory(), "pki/own/certs/OpcTestClient.pfx");
            string pfxPassword = ""; 
            string userName = "mngr";
            string password = "mngr";

            Directory.CreateDirectory(Path.GetDirectoryName(pfxPath)!);
            Directory.CreateDirectory("pki/trusted/certs");
            Directory.CreateDirectory("pki/issuers/certs");
            Directory.CreateDirectory("pki/rejected/certs");

            X509Certificate2? clientCert = new X509Certificate2(pfxPath, pfxPassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);

            var config = new ApplicationConfiguration {
                ApplicationName = "OpcTestClient",
                ApplicationType = ApplicationType.Client,
                ApplicationUri = applicationUri,
                SecurityConfiguration = new SecurityConfiguration {
                    ApplicationCertificate = new CertificateIdentifier { Certificate = clientCert },
                    TrustedPeerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = Path.GetFullPath("pki/trusted") },
                    TrustedIssuerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = Path.GetFullPath("pki/issuers") },
                    RejectedCertificateStore = new CertificateTrustList { StoreType = "Directory", StorePath = Path.GetFullPath("pki/rejected") },
                    AutoAcceptUntrustedCertificates = true,
                    AddAppCertToTrustedStore = true
                },
                TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
                ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
            };

            await config.ValidateAsync(ApplicationType.Client);
            config.CertificateValidator.CertificateValidation += (v, e) => { if (e.Error.StatusCode != StatusCodes.Good) e.Accept = true; };

            ISession? session = null;
            try
            {
                Console.WriteLine($"Step 1: Connecting to {endpointUrl}...");
                var endpointConfig = EndpointConfiguration.Create(config);
                using var discovery = await DiscoveryClient.CreateAsync(config, new Uri(endpointUrl), DiagnosticsMasks.All, CancellationToken.None);
                var endpoints = await discovery.GetEndpointsAsync(null);
                var selected = endpoints.OrderByDescending(e => e.SecurityLevel)
                                       .FirstOrDefault(e => e.SecurityPolicyUri.Contains("Basic256Sha256")) ?? endpoints[0];
                
                var endpoint = new ConfiguredEndpoint(null, selected, endpointConfig);
                var identity = new UserIdentity(userName, Encoding.UTF8.GetBytes(password));
                
                session = await Session.Create(config, endpoint, false, "OpcTestSession", 60000, identity, null);
                Console.WriteLine($"✅ Connected! SessionID: {session.SessionId}");

                // 3. 데이터 읽기 및 DB 저장 루프 (테스트용으로 5회 반복)
                string nodeID = "ns=1;s=shinam:p-6102.hzset.fieldvalue";
                for (int i = 0; i < 5; i++)
                {
                    var result = await session.ReadValueAsync(nodeID);
                    double val = Convert.ToDouble(result.Value);
                    string status = result.StatusCode.ToString();

                    Console.WriteLine($"[{i+1}] TAG: {val} (Status: {status})");
                    
                    // DB에 저장
                    await SaveToDatabase("p-6102", val, status);
                    
                    await Task.Delay(2000); // 2초 대기
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ 오류 발생: {ex.Message}");
            }
            finally { if (session != null) { await session.CloseAsync(); session.Dispose(); } }
            Console.WriteLine("\n작업 완료. 엔터를 누르세요...");
            Console.ReadLine();
        }
    }
}