using Microsoft.Extensions.DependencyInjection;
using RackPeek.Domain.Resources.Switches;
using RackPeek.Domain.UseCases.Ports;
using Spectre.Console;
using Spectre.Console.Cli;

namespace Shared.Rcl.Commands.Switches.Ports;

public class SwitchPortUpdateSettings : SwitchNameSettings {
    [CommandOption("--index <INDEX>")] public int Index { get; set; }

    [CommandOption("--type")] public string? Type { get; set; }

    [CommandOption("--speed")] public double? Speed { get; set; }

    [CommandOption("--count")] public int? Count { get; set; }
}

public class SwitchPortUpdateCommand(IServiceProvider sp)
    : AsyncCommand<SwitchPortUpdateSettings> {
    protected override async Task<int> ExecuteAsync(CommandContext ctx, SwitchPortUpdateSettings s, CancellationToken ct) {
        using IServiceScope scope = sp.CreateScope();
        IUpdatePortUseCase<Switch> useCase = scope.ServiceProvider.GetRequiredService<IUpdatePortUseCase<Switch>>();

        await useCase.ExecuteAsync(s.Name, s.Index, s.Type, s.Speed, s.Count);

        AnsiConsole.MarkupLine($"[green]Port {s.Index} updated on switch '{s.Name}'.[/]");
        return 0;
    }
}
