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

namespace ScadaLink.CLI.Commands;

public static class NotificationCommands
{
    public static Command Build(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var command = new Command("notification") { Description = "Manage notification lists" };

        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));
        command.Add(BuildSmtp(urlOption, formatOption, usernameOption, passwordOption));

        return command;
    }

    private static Command BuildGet(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var idOption = new Option<int>("--id") { Description = "Notification list ID", Required = true };
        var cmd = new Command("get") { Description = "Get a notification list 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 GetNotificationListCommand(id));
        });
        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 = "Notification list ID", Required = true };
        var nameOption = new Option<string>("--name") { Description = "List name", Required = true };
        var emailsOption = new Option<string>("--emails") { Description = "Comma-separated recipient emails", Required = true };

        var cmd = new Command("update") { Description = "Update a notification list" };
        cmd.Add(idOption);
        cmd.Add(nameOption);
        cmd.Add(emailsOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            var name = result.GetValue(nameOption)!;
            var emailsRaw = result.GetValue(emailsOption)!;
            var emails = emailsRaw.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new UpdateNotificationListCommand(id, name, emails));
        });
        return cmd;
    }

    private static Command BuildSmtp(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var group = new Command("smtp") { Description = "Manage SMTP configuration" };

        var listCmd = new Command("list") { Description = "List SMTP configurations" };
        listCmd.SetAction(async (ParseResult result) =>
        {
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new ListSmtpConfigsCommand());
        });
        group.Add(listCmd);

        var idOption = new Option<int>("--id") { Description = "SMTP config ID", Required = true };
        var serverOption = new Option<string>("--server") { Description = "SMTP server", Required = true };
        var portOption = new Option<int>("--port") { Description = "SMTP port", Required = true };
        var authModeOption = new Option<string>("--auth-mode") { Description = "Auth mode", Required = true };
        var fromOption = new Option<string>("--from-address") { Description = "From email address", Required = true };
        var updateCmd = new Command("update") { Description = "Update SMTP configuration" };
        updateCmd.Add(idOption);
        updateCmd.Add(serverOption);
        updateCmd.Add(portOption);
        updateCmd.Add(authModeOption);
        updateCmd.Add(fromOption);
        updateCmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            var server = result.GetValue(serverOption)!;
            var port = result.GetValue(portOption);
            var authMode = result.GetValue(authModeOption)!;
            var from = result.GetValue(fromOption)!;
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new UpdateSmtpConfigCommand(id, server, port, authMode, from));
        });
        group.Add(updateCmd);

        return group;
    }

    private static Command BuildList(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var cmd = new Command("list") { Description = "List all notification lists" };
        cmd.SetAction(async (ParseResult result) =>
        {
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new ListNotificationListsCommand());
        });
        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 = "Notification list name", Required = true };
        var emailsOption = new Option<string>("--emails") { Description = "Comma-separated recipient emails", Required = true };

        var cmd = new Command("create") { Description = "Create a notification list" };
        cmd.Add(nameOption);
        cmd.Add(emailsOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var name = result.GetValue(nameOption)!;
            var emailsRaw = result.GetValue(emailsOption)!;
            var emails = emailsRaw.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new CreateNotificationListCommand(name, emails));
        });
        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 = "Notification list ID", Required = true };
        var cmd = new Command("delete") { Description = "Delete a notification list" };
        cmd.Add(idOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var id = result.GetValue(idOption);
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new DeleteNotificationListCommand(id));
        });
        return cmd;
    }
}
