using Microsoft.Extensions.DependencyInjection;
using RackPeek.Domain.Resources.AccessPoints;
using Shared.Rcl.Commands;
using Spectre.Console;
using Spectre.Console.Cli;

namespace Shared.Rcl.Commands.AccessPoints;

public class AccessPointGetCommand(
    IServiceProvider serviceProvider
) : AsyncCommand {
    protected override async Task<int> ExecuteAsync(
        CommandContext context,
        CancellationToken cancellationToken) {
        using IServiceScope scope = serviceProvider.CreateScope();
        AccessPointHardwareReportUseCase useCase =
            scope.ServiceProvider.GetRequiredService<AccessPointHardwareReportUseCase>();

        AccessPointHardwareReport report = await useCase.ExecuteAsync();

        if (report.AccessPoints.Count == 0) {
            AnsiConsole.MarkupLine("[yellow]No access points found.[/]");
            return 0;
        }

        Table table = new Table()
            .Border(TableBorder.Rounded)
            .AddColumn("Name")
            .AddColumn("Model")
            .AddColumn("Speed (Gbps)");

        foreach (AccessPointHardwareRow ap in report.AccessPoints)
            table.AddRow(
                ap.Name.EscapeMarkup(),
                ap.Model.EscapeMarkup(),
                ap.SpeedGb.ToString()
            );


        AnsiConsole.Write(table);
        return 0;
    }
}
