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

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

/// <summary>
///     Probes an S7 endpoint: connects via S7.Net, reads one merker word, prints health.
///     If the PLC is fresh out of TIA Portal the probe will surface
///     <c>BadNotSupported</c> — PUT/GET communication has to be enabled in the hardware
///     config for any S7-1200/1500 for the driver to get past the handshake.
/// </summary>
[Command("probe", Description = "Verify the S7 endpoint is reachable and a sample read succeeds.")]
public sealed class ProbeCommand : S7CommandBase
{
    [CommandOption("address", 'a', Description =
        "Probe address (default MW0 — merker word 0). DB1.DBW0 if your PLC project " +
        "reserves a fingerprint DB.")]
    public string Address { get; init; } = "MW0";

    [CommandOption("type", Description = "Probe data type (default Int16).")]
    public S7DataType DataType { get; init; } = S7DataType.Int16;

    public override async ValueTask ExecuteAsync(IConsole console)
    {
        ConfigureLogging();
        var ct = console.RegisterCancellationHandler();

        var probeTag = new S7TagDefinition(
            Name: "__probe",
            Address: Address,
            DataType: DataType,
            Writable: false);
        var options = BuildOptions([probeTag]);

        await using var driver = new S7Driver(options, DriverInstanceId);
        try
        {
            await driver.InitializeAsync("{}", ct);
            var snapshot = await driver.ReadAsync(["__probe"], ct);
            var health = driver.GetHealth();

            await console.Output.WriteLineAsync($"Host:         {Host}:{Port}");
            await console.Output.WriteLineAsync($"CPU:          {CpuType} rack={Rack} slot={Slot}");
            await console.Output.WriteLineAsync($"Health:       {health.State}");
            if (health.LastError is { } err)
                await console.Output.WriteLineAsync($"Last error:   {err}");
            await console.Output.WriteLineAsync();
            await console.Output.WriteLineAsync(SnapshotFormatter.Format(Address, snapshot[0]));
        }
        finally
        {
            await driver.ShutdownAsync(CancellationToken.None);
        }
    }
}
