using CliFx.Attributes;
using CliFx.Infrastructure;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common;

namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Cli.Commands;

/// <summary>
///     Watch a FOCAS address via polled subscription until Ctrl+C. FOCAS has no push
///     model; <c>PollGroupEngine</c> handles the tick loop.
/// </summary>
[Command("subscribe", Description = "Watch a FOCAS address via polled subscription until Ctrl+C.")]
public sealed class SubscribeCommand : FocasCommandBase
{
    /// <summary>Gets the FOCAS address to subscribe to.</summary>
    [CommandOption("address", 'a', Description = "FOCAS address — same format as `read`.", IsRequired = true)]
    public string Address { get; init; } = default!;

    /// <summary>Gets the data type of the address.</summary>
    [CommandOption("type", 't', Description =
        "Bit / Byte / Int16 / Int32 / Float32 / Float64 / String (default Int16).")]
    public FocasDataType DataType { get; init; } = FocasDataType.Int16;

    /// <summary>Gets the polling interval in milliseconds.</summary>
    [CommandOption("interval-ms", 'i', Description = "Publishing interval ms (default 1000).")]
    public int IntervalMs { get; init; } = 1000;

    /// <inheritdoc />
    public override async ValueTask ExecuteAsync(IConsole console)
    {
        ConfigureLogging();
        // Validate numeric option ranges (including the subscribe-only
        // --interval-ms) before any driver work so a zero/negative interval surfaces as a
        // clean CommandException rather than a tight-spinning poll loop.
        ValidateOptions(IntervalMs);
        var ct = console.RegisterCancellationHandler();

        var tagName = ReadCommand.SynthesiseTagName(Address, DataType);
        var tag = new FocasTagDefinition(
            Name: tagName,
            DeviceHostAddress: HostAddress,
            Address: Address,
            DataType: DataType,
            Writable: false);
        var options = BuildOptions([tag]);

        // `await using` is the sole driver-disposal mechanism — FocasDriver.DisposeAsync
        // already invokes ShutdownAsync, so a redundant ShutdownAsync(CancellationToken.None) in finally
        // ran shutdown twice. Only UnsubscribeAsync stays in the finally block — that's a subscription
        // lifecycle concern that is not part of driver disposal.
        await using var driver = new FocasDriver(options, DriverInstanceId);
        // Serialize console writes from the PollGroupEngine background
        // thread so overlapping poll ticks (and the "Subscribed to ..." banner from the CliFx
        // invocation thread) can't interleave partial lines.
        var writeLock = new object();
        ISubscriptionHandle? handle = null;
        try
        {
            await driver.InitializeAsync("{}", ct);

            // Route every data-change event to the CliFx console (not
            // System.Console — the analyzer flags it + IConsole is the testable abstraction).
            // The handler is synchronous because OnDataChange is raised from a driver
            // background thread; the IConsole.Output writer is not documented as thread-safe
            // so we serialize against the banner write via writeLock.
            driver.OnDataChange += (_, e) =>
            {
                // Swallow + log write failures so a transient stdout error (closed pipe, IO
                // exception on a redirected stream) cannot tear down the poll-engine
                // background loop. Without this guard the unhandled exception would fault
                // the long-running subscribe.
                try
                {
                    var line = $"[{DateTime.UtcNow:HH:mm:ss.fff}] " +
                        $"{e.FullReference} = {SnapshotFormatter.FormatValue(e.Snapshot.Value)} " +
                        $"({SnapshotFormatter.FormatStatus(e.Snapshot.StatusCode)})";
                    lock (writeLock)
                    {
                        console.Output.WriteLine(line);
                    }
                }
                catch (Exception ex)
                {
                    Serilog.Log.Logger.Warning(ex,
                        "SubscribeCommand: console write failed for {Tag}; continuing poll loop.",
                        e.FullReference);
                }
            };

            handle = await driver.SubscribeAsync([tagName], TimeSpan.FromMilliseconds(IntervalMs), ct);

            // Hold the lock around the banner write so the first
            // poll-driven change line from the driver tick thread can't interleave with
            // this banner.
            lock (writeLock)
            {
                console.Output.WriteLine(
                    $"Subscribed to {Address} @ {IntervalMs}ms. Ctrl+C to stop.");
            }
            try
            {
                await Task.Delay(System.Threading.Timeout.InfiniteTimeSpan, ct);
            }
            catch (OperationCanceledException)
            {
                // Expected on Ctrl+C.
            }
        }
        finally
        {
            if (handle is not null)
            {
                // Stop the subscription before disposal — UnsubscribeAsync
                // halts the poll-group ticker so no further OnDataChange events fire. The
                // anonymous handler is never explicitly removed via -=; instead, driver disposal
                // (via `await using` immediately after this finally) tears down the PollGroupEngine,
                // which is the real cleanup. The unsubscribe here is a best-effort subscription
                // lifecycle step so the CNC-side session is cleanly released before teardown.
                try { await driver.UnsubscribeAsync(handle, CancellationToken.None); }
                catch { /* teardown best-effort */ }
            }
            // Flush Serilog before process exit so buffered log output emitted during driver
            // shutdown is not silently dropped (matching DriverCommandBase docs and every
            // sibling CLI — Modbus / AbCip / AbLegacy / TwinCAT).
            FlushLogging();
        }
    }
}
