using System.CommandLine;
using System.CommandLine.Parsing;
using ScadaLink.Commons.Messages.Management;

namespace ScadaLink.CLI.Commands;

public static class SharedScriptCommands
{
    public static Command Build(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var command = new Command("shared-script") { Description = "Manage shared scripts" };

        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 shared scripts" };
        cmd.SetAction(async (ParseResult result) =>
        {
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new ListSharedScriptsCommand());
        });
        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 = "Shared script ID", Required = true };
        var cmd = new Command("get") { Description = "Get a shared script 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 GetSharedScriptCommand(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 = "Script name", Required = true };
        var codeOption = new Option<string>("--code") { Description = "Script code", Required = true };
        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 a shared script" };
        cmd.Add(nameOption);
        cmd.Add(codeOption);
        cmd.Add(parametersOption);
        cmd.Add(returnDefOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var name = result.GetValue(nameOption)!;
            var code = result.GetValue(codeOption)!;
            var parameters = result.GetValue(parametersOption);
            var returnDef = result.GetValue(returnDefOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new CreateSharedScriptCommand(name, code, 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 = "Shared script ID", Required = true };
        var nameOption = new Option<string>("--name") { Description = "Script name", Required = true };
        var codeOption = new Option<string>("--code") { Description = "Script code", Required = true };
        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 a shared script" };
        cmd.Add(idOption);
        cmd.Add(nameOption);
        cmd.Add(codeOption);
        cmd.Add(parametersOption);
        cmd.Add(returnDefOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            var name = result.GetValue(nameOption)!;
            var code = result.GetValue(codeOption)!;
            var parameters = result.GetValue(parametersOption);
            var returnDef = result.GetValue(returnDefOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new UpdateSharedScriptCommand(id, name, code, 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 = "Shared script ID", Required = true };
        var cmd = new Command("delete") { Description = "Delete a shared script" };
        cmd.Add(idOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new DeleteSharedScriptCommand(id));
        });
        return cmd;
    }
}
