using System.Text.Json;
using System.Text.Json.Serialization;
using ZB.MOM.WW.OtOpcUa.Core.Hosting;

namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip;

/// <summary>
///     Static factory registration helper for <see cref="AbCipDriver"/>. Server's Program.cs
///     calls <see cref="Register"/> once at startup; the bootstrapper (task #248) then
///     materialises AB CIP DriverInstance rows from the central config DB into live driver
///     instances. Mirrors <c>GalaxyProxyDriverFactoryExtensions</c>.
/// </summary>
public static class AbCipDriverFactoryExtensions
{
    public const string DriverTypeName = "AbCip";

    public static void Register(DriverFactoryRegistry registry)
    {
        ArgumentNullException.ThrowIfNull(registry);
        registry.Register(DriverTypeName, CreateInstance);
    }

    internal static AbCipDriver CreateInstance(string driverInstanceId, string driverConfigJson)
    {
        ArgumentException.ThrowIfNullOrWhiteSpace(driverInstanceId);
        ArgumentException.ThrowIfNullOrWhiteSpace(driverConfigJson);

        var dto = JsonSerializer.Deserialize<AbCipDriverConfigDto>(driverConfigJson, JsonOptions)
            ?? throw new InvalidOperationException(
                $"AB CIP driver config for '{driverInstanceId}' deserialised to null");

        var options = new AbCipDriverOptions
        {
            Devices = dto.Devices is { Count: > 0 }
                ? [.. dto.Devices.Select(d => new AbCipDeviceOptions(
                    HostAddress: d.HostAddress ?? throw new InvalidOperationException(
                        $"AB CIP config for '{driverInstanceId}' has a device missing HostAddress"),
                    PlcFamily: ParseEnum<AbCipPlcFamily>(d.PlcFamily, "device", driverInstanceId, "PlcFamily",
                        fallback: AbCipPlcFamily.ControlLogix),
                    DeviceName: d.DeviceName))]
                : [],
            Tags = dto.Tags is { Count: > 0 }
                ? [.. dto.Tags.Select(t => BuildTag(t, driverInstanceId))]
                : [],
            Probe = new AbCipProbeOptions
            {
                Enabled = dto.Probe?.Enabled ?? true,
                Interval = TimeSpan.FromMilliseconds(dto.Probe?.IntervalMs ?? 5_000),
                Timeout = TimeSpan.FromMilliseconds(dto.Probe?.TimeoutMs ?? 2_000),
                ProbeTagPath = dto.Probe?.ProbeTagPath,
            },
            Timeout = TimeSpan.FromMilliseconds(dto.TimeoutMs ?? 2_000),
            EnableControllerBrowse = dto.EnableControllerBrowse ?? false,
            EnableAlarmProjection = dto.EnableAlarmProjection ?? false,
            AlarmPollInterval = TimeSpan.FromMilliseconds(dto.AlarmPollIntervalMs ?? 1_000),
        };

        return new AbCipDriver(options, driverInstanceId);
    }

    private static AbCipTagDefinition BuildTag(AbCipTagDto t, string driverInstanceId) =>
        new(
            Name: t.Name ?? throw new InvalidOperationException(
                $"AB CIP config for '{driverInstanceId}' has a tag missing Name"),
            DeviceHostAddress: t.DeviceHostAddress ?? throw new InvalidOperationException(
                $"AB CIP tag '{t.Name}' in '{driverInstanceId}' missing DeviceHostAddress"),
            TagPath: t.TagPath ?? throw new InvalidOperationException(
                $"AB CIP tag '{t.Name}' in '{driverInstanceId}' missing TagPath"),
            DataType: ParseEnum<AbCipDataType>(t.DataType, t.Name, driverInstanceId, "DataType"),
            Writable: t.Writable ?? true,
            WriteIdempotent: t.WriteIdempotent ?? false,
            Members: t.Members is { Count: > 0 }
                ? [.. t.Members.Select(m => new AbCipStructureMember(
                    Name: m.Name ?? throw new InvalidOperationException(
                        $"AB CIP tag '{t.Name}' in '{driverInstanceId}' has a member missing Name"),
                    DataType: ParseEnum<AbCipDataType>(m.DataType, t.Name, driverInstanceId,
                        $"Members[{m.Name}].DataType"),
                    Writable: m.Writable ?? true,
                    WriteIdempotent: m.WriteIdempotent ?? false))]
                : null,
            SafetyTag: t.SafetyTag ?? false);

    private static T ParseEnum<T>(string? raw, string? tagName, string driverInstanceId, string field,
        T? fallback = null) where T : struct, Enum
    {
        if (string.IsNullOrWhiteSpace(raw))
        {
            if (fallback.HasValue) return fallback.Value;
            throw new InvalidOperationException(
                $"AB CIP tag '{tagName ?? "<unnamed>"}' in '{driverInstanceId}' missing {field}");
        }
        return Enum.TryParse<T>(raw, ignoreCase: true, out var v)
            ? v
            : throw new InvalidOperationException(
                $"AB CIP tag '{tagName}' has unknown {field} '{raw}'. " +
                $"Expected one of {string.Join(", ", Enum.GetNames<T>())}");
    }

    private static readonly JsonSerializerOptions JsonOptions = new()
    {
        PropertyNameCaseInsensitive = true,
        ReadCommentHandling = JsonCommentHandling.Skip,
        AllowTrailingCommas = true,
    };

    internal sealed class AbCipDriverConfigDto
    {
        public int? TimeoutMs { get; init; }
        public bool? EnableControllerBrowse { get; init; }
        public bool? EnableAlarmProjection { get; init; }
        public int? AlarmPollIntervalMs { get; init; }
        public List<AbCipDeviceDto>? Devices { get; init; }
        public List<AbCipTagDto>? Tags { get; init; }
        public AbCipProbeDto? Probe { get; init; }
    }

    internal sealed class AbCipDeviceDto
    {
        public string? HostAddress { get; init; }
        public string? PlcFamily { get; init; }
        public string? DeviceName { get; init; }
    }

    internal sealed class AbCipTagDto
    {
        public string? Name { get; init; }
        public string? DeviceHostAddress { get; init; }
        public string? TagPath { get; init; }
        public string? DataType { get; init; }
        public bool? Writable { get; init; }
        public bool? WriteIdempotent { get; init; }
        public List<AbCipMemberDto>? Members { get; init; }
        public bool? SafetyTag { get; init; }
    }

    internal sealed class AbCipMemberDto
    {
        public string? Name { get; init; }
        public string? DataType { get; init; }
        public bool? Writable { get; init; }
        public bool? WriteIdempotent { get; init; }
    }

    internal sealed class AbCipProbeDto
    {
        public bool? Enabled { get; init; }
        public int? IntervalMs { get; init; }
        public int? TimeoutMs { get; init; }
        public string? ProbeTagPath { get; init; }
    }
}
