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

namespace Shared.Rcl.Commands.Servers;

public class ServerAddSettings : CommandSettings {
    [CommandArgument(0, "<name>")] public string Name { get; set; } = default!;
}

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

        await useCase.ExecuteAsync(
            settings.Name
        );

        AnsiConsole.MarkupLine($"[green]Server '{settings.Name}' added.[/]");
        return 0;
    }
}
