using System.CommandLine;
using System.CommandLine.Parsing;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;

namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;

public static class ApiMethodCommands
{
    /// <summary>
    /// Builds the <c>api-method</c> CLI command group with subcommands for managing inbound API methods.
    /// </summary>
    /// <param name="urlOption">Global option for the management URL.</param>
    /// <param name="formatOption">Global option for the output format.</param>
    /// <param name="usernameOption">Global option for the authentication username.</param>
    /// <param name="passwordOption">Global option for the authentication password.</param>
    /// <returns>The configured <c>api-method</c> command with all subcommands registered.</returns>
    public static Command Build(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var command = new Command("api-method") { Description = "Manage inbound API methods" };

        command.Add(BuildList(urlOption, formatOption, usernameOption, passwordOption));
        command.Add(BuildGet(urlOption, formatOption, usernameOption, passwordOption));
        command.Add(BuildCreate(urlOption, formatOption, usernameOption, passwordOption));
        command.Add(BuildUpdate(urlOption, formatOption, usernameOption, passwordOption));
        command.Add(BuildDelete(urlOption, formatOption, usernameOption, passwordOption));

        return command;
    }

    private static Command BuildList(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var cmd = new Command("list") { Description = "List all API methods" };
        cmd.SetAction(async (ParseResult result) =>
        {
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new ListApiMethodsCommand());
        });
        return cmd;
    }

    private static Command BuildGet(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var idOption = new Option<int>("--id") { Description = "API method ID", Required = true };
        var cmd = new Command("get") { Description = "Get an API method by ID" };
        cmd.Add(idOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new GetApiMethodCommand(id));
        });
        return cmd;
    }

    private static Command BuildCreate(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var nameOption = new Option<string>("--name") { Description = "Method name", Required = true };
        var scriptOption = new Option<string>("--script") { Description = "Script code", Required = true };
        var timeoutOption = new Option<int>("--timeout") { Description = "Timeout in seconds" };
        timeoutOption.DefaultValueFactory = _ => 30;
        var parametersOption = new Option<string?>("--parameters") { Description = "Parameter definitions JSON" };
        var returnDefOption = new Option<string?>("--return-def") { Description = "Return type definition" };

        var cmd = new Command("create") { Description = "Create an API method" };
        cmd.Add(nameOption);
        cmd.Add(scriptOption);
        cmd.Add(timeoutOption);
        cmd.Add(parametersOption);
        cmd.Add(returnDefOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var name = result.GetValue(nameOption)!;
            var script = result.GetValue(scriptOption)!;
            var timeout = result.GetValue(timeoutOption);
            var parameters = result.GetValue(parametersOption);
            var returnDef = result.GetValue(returnDefOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new CreateApiMethodCommand(name, script, timeout, parameters, returnDef));
        });
        return cmd;
    }

    private static Command BuildUpdate(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var idOption = new Option<int>("--id") { Description = "API method ID", Required = true };
        var scriptOption = new Option<string>("--script") { Description = "Script code", Required = true };
        var timeoutOption = new Option<int>("--timeout") { Description = "Timeout in seconds" };
        timeoutOption.DefaultValueFactory = _ => 30;
        var parametersOption = new Option<string?>("--parameters") { Description = "Parameter definitions JSON" };
        var returnDefOption = new Option<string?>("--return-def") { Description = "Return type definition" };

        var cmd = new Command("update") { Description = "Update an API method" };
        cmd.Add(idOption);
        cmd.Add(scriptOption);
        cmd.Add(timeoutOption);
        cmd.Add(parametersOption);
        cmd.Add(returnDefOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            var script = result.GetValue(scriptOption)!;
            var timeout = result.GetValue(timeoutOption);
            var parameters = result.GetValue(parametersOption);
            var returnDef = result.GetValue(returnDefOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new UpdateApiMethodCommand(id, script, timeout, parameters, returnDef));
        });
        return cmd;
    }

    private static Command BuildDelete(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var idOption = new Option<int>("--id") { Description = "API method ID", Required = true };
        var cmd = new Command("delete") { Description = "Delete an API method" };
        cmd.Add(idOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new DeleteApiMethodCommand(id));
        });
        return cmd;
    }
}
