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

namespace Shared.Rcl.Commands.Systems;

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

        SystemReport report = await useCase.ExecuteAsync();

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

        Table table = new Table()
            .Border(TableBorder.Rounded)
            .AddColumn("Name")
            .AddColumn("Type")
            .AddColumn("OS")
            .AddColumn("Cores")
            .AddColumn("RAM (GB)")
            .AddColumn("Storage (GB)")
            .AddColumn("Runs On");

        foreach (SystemReportRow s in report.Systems)
            table.AddRow(
                s.Name.EscapeMarkup(),
                (s.Type ?? "Unknown").EscapeMarkup(),
                (s.Os ?? "Unknown").EscapeMarkup(),
                s.Cores.ToString(),
                s.RamGb.ToString(),
                s.TotalStorageGb.ToString(),
                (string.Join(", ", s.RunsOn) ?? "Unkown").EscapeMarkup()
            );

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