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

namespace Shared.Rcl.Commands.AccessPoints;

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

        AccessPoint ap = await useCase.ExecuteAsync(settings.Name);

        Grid grid = new Grid()
            .AddColumn(new GridColumn().NoWrap())
            .AddColumn(new GridColumn().NoWrap());

        grid.AddRow("Name:", ap.Name.EscapeMarkup());
        grid.AddRow("Model:", (ap.Model ?? "Unknown").EscapeMarkup());
        grid.AddRow("Speed (Gbps):", (ap.Speed?.ToString() ?? "Unknown").EscapeMarkup());

        if (ap.Labels.Count > 0)
            grid.AddRow("Labels:", string.Join(", ", ap.Labels.Select(kvp => $"{kvp.Key.EscapeMarkup()}: {kvp.Value.EscapeMarkup()}")));

        AnsiConsole.Write(
            new Panel(grid)
                .Header("Access Point")
                .Border(BoxBorder.Rounded));

        return 0;
    }
}
