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

namespace ScadaLink.CLI.Commands;

public static class DbConnectionCommands
{
    public static Command Build(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var command = new Command("db-connection") { Description = "Manage database connections" };

        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 database connections" };
        cmd.SetAction(async (ParseResult result) =>
        {
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new ListDatabaseConnectionsCommand());
        });
        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 = "Database connection ID", Required = true };
        var cmd = new Command("get") { Description = "Get a database connection 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 GetDatabaseConnectionCommand(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 = "Connection name", Required = true };
        var connStrOption = new Option<string>("--connection-string") { Description = "Connection string", Required = true };

        var cmd = new Command("create") { Description = "Create a database connection" };
        cmd.Add(nameOption);
        cmd.Add(connStrOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var name = result.GetValue(nameOption)!;
            var connStr = result.GetValue(connStrOption)!;
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new CreateDatabaseConnectionDefCommand(name, connStr));
        });
        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 = "Database connection ID", Required = true };
        var nameOption = new Option<string>("--name") { Description = "Connection name", Required = true };
        var connStrOption = new Option<string>("--connection-string") { Description = "Connection string", Required = true };

        var cmd = new Command("update") { Description = "Update a database connection" };
        cmd.Add(idOption);
        cmd.Add(nameOption);
        cmd.Add(connStrOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            var name = result.GetValue(nameOption)!;
            var connStr = result.GetValue(connStrOption)!;
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new UpdateDatabaseConnectionDefCommand(id, name, connStr));
        });
        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 = "Database connection ID", Required = true };
        var cmd = new Command("delete") { Description = "Delete a database connection" };
        cmd.Add(idOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new DeleteDatabaseConnectionDefCommand(id));
        });
        return cmd;
    }
}
