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

namespace Shared.Rcl.Commands.Desktops;

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

        Desktop desktop = await useCase.ExecuteAsync(settings.Name);

        AnsiConsole.MarkupLine($"[green]{desktop.Name}[/] (Model: {desktop.Model ?? "Unknown"})");
        return 0;
    }
}
