using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Infrastructure;
using Opc.Ua;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Helpers;
using ZB.MOM.WW.OtOpcUa.Client.Shared;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Models;

namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;

[Command("shelve", Description = "Shelve or unshelve an active alarm condition (OPC UA Part 9 ShelvedStateMachine)")]
public class ShelveCommand : CommandBase
{
    /// <summary>
    /// Creates the shelve command used to shelve or unshelve an OPC UA alarm condition from the terminal.
    /// </summary>
    /// <param name="factory">The factory that creates the shared client service for the command run.</param>
    public ShelveCommand(IOpcUaClientServiceFactory factory) : base(factory)
    {
    }

    /// <summary>
    /// Gets the condition node ID of the alarm to shelve or unshelve.
    /// </summary>
    [CommandOption("node", 'n', Description = "Condition node ID of the alarm to shelve/unshelve", IsRequired = true)]
    public string NodeId { get; init; } = default!;

    /// <summary>
    /// Gets the shelve operation kind: OneShot, Timed, or Unshelve.
    /// </summary>
    [CommandOption("kind", 'k', Description = "Shelve operation: OneShot | Timed | Unshelve", IsRequired = true)]
    public string Kind { get; init; } = default!;

    /// <summary>
    /// Gets the shelving duration in seconds for Timed shelving (must be &gt; 0; ignored for OneShot and Unshelve).
    /// The value is passed in seconds by the operator and converted to milliseconds for the OPC UA TimedShelve call.
    /// </summary>
    [CommandOption("duration", 'd', Description = "Shelving duration in seconds (must be > 0; in seconds, converted to milliseconds for the OPC UA call; required for --kind Timed)")]
    public double DurationSeconds { get; init; }

    /// <inheritdoc />
    public override async ValueTask ExecuteAsync(IConsole console)
    {
        ConfigureLogging();

        try
        {
            NodeIdParser.ParseRequired(NodeId);
        }
        catch (Exception ex) when (ex is FormatException or ArgumentException)
        {
            throw new CommandException($"Invalid --node value: {ex.Message}");
        }

        if (!Enum.TryParse<ShelveKind>(Kind, ignoreCase: true, out var shelveKind))
            throw new CommandException(
                $"Invalid --kind value '{Kind}'. Expected one of: OneShot, Timed, Unshelve.");

        if (shelveKind == ShelveKind.Timed && DurationSeconds <= 0)
            throw new CommandException(
                "--duration must be greater than 0 when --kind is Timed.");

        IOpcUaClientService? service = null;
        try
        {
            var ct = console.RegisterCancellationHandler();
            (service, _) = await CreateServiceAndConnectAsync(ct);

            var statusCode = await service.ShelveAlarmAsync(NodeId, shelveKind, DurationSeconds, ct);

            if (StatusCode.IsGood(statusCode))
                await console.Output.WriteLineAsync($"{shelveKind} successful: {NodeId}");
            else
                await console.Output.WriteLineAsync($"{shelveKind} failed: {statusCode}");
        }
        finally
        {
            if (service != null)
            {
                await service.DisconnectAsync();
                service.Dispose();
            }
        }
    }
}
