using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using RackPeek.Domain.Resources.Services.UseCases;
using Shared.Rcl.Commands.Servers;
using Spectre.Console;
using Spectre.Console.Cli;

namespace Shared.Rcl.Commands.Services;

public class ServiceSetSettings : ServerNameSettings {
    [CommandOption("--ip")]
    [Description("The ip address of the service.")]
    public string? Ip { get; set; }

    [CommandOption("--port")]
    [Description("The port the service is running on.")]
    public int? Port { get; set; }

    [CommandOption("--protocol")]
    [Description("The service protocol.")]
    public string? Protocol { get; set; }

    [CommandOption("--url")]
    [Description("The service URL.")]
    public string? Url { get; set; }

    [CommandOption("--runs-on <RUNSON>")]
    [Description("The system(s) the service is running on.")]
    public string[]? RunsOn { get; set; }
}

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

        await useCase.ExecuteAsync(
            settings.Name,
            settings.Ip,
            settings.Port,
            settings.Protocol,
            settings.Url,
            settings.RunsOn?.ToList()
        );

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