using CoreMod.Services;
using Share;
using Share.Helper;
using System.ComponentModel;

namespace CommandLine.Commands;

/// <summary>
/// Install module command
/// </summary>
public class InstallCommand(
    ModuleInstallService moduleInstallService,
    SolutionContext projectContext
) : AsyncCommand<InstallCommand.Settings>
{
    public class Settings : CommandSettings
    {
        [CommandArgument(0, "<PackagePath>")]
        [Description("Path to the module package zip file")]
        public required string PackagePath { get; set; }

        [CommandArgument(1, "<ServiceName>")]
        [Description("Service name in Services directory")]
        public required string ServiceName { get; set; }
    }

    public override async Task<int> ExecuteAsync(
        CommandContext context,
        Settings settings,
        CancellationToken cancellationToken
    )
    {
        var currentDirectory = Environment.CurrentDirectory;
        await projectContext.SetSolutionAsync(currentDirectory);
        // Ensure we have a valid project context
        if (string.IsNullOrEmpty(projectContext.SolutionPath))
        {
            OutputHelper.Error("Error: Not in a valid solution directory");
            return 1;
        }

        var success = await moduleInstallService.InstallModuleAsync(
            settings.PackagePath,
            settings.ServiceName
        );

        return success ? 0 : 1;
    }
}
