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

namespace ScadaLink.CLI.Commands;

public static class HealthCommands
{
    public static Command Build(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var command = new Command("health") { Description = "Health monitoring" };

        command.Add(BuildSummary(urlOption, formatOption, usernameOption, passwordOption));
        command.Add(BuildSite(urlOption, formatOption, usernameOption, passwordOption));
        command.Add(BuildEventLog(urlOption, formatOption, usernameOption, passwordOption));
        command.Add(BuildParkedMessages(urlOption, formatOption, usernameOption, passwordOption));

        return command;
    }

    private static Command BuildSummary(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var cmd = new Command("summary") { Description = "Get system health summary" };
        cmd.SetAction(async (ParseResult result) =>
        {
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new GetHealthSummaryCommand());
        });
        return cmd;
    }

    private static Command BuildSite(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var identifierOption = new Option<string>("--identifier") { Description = "Site identifier", Required = true };
        var cmd = new Command("site") { Description = "Get health for a specific site" };
        cmd.Add(identifierOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            var identifier = result.GetValue(identifierOption)!;
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption, new GetSiteHealthCommand(identifier));
        });
        return cmd;
    }

    private static Command BuildEventLog(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var siteOption = new Option<string>("--site") { Description = "Site identifier", Required = true };
        var eventTypeOption = new Option<string?>("--event-type") { Description = "Filter by event type" };
        var severityOption = new Option<string?>("--severity") { Description = "Filter by severity" };
        var keywordOption = new Option<string?>("--keyword") { Description = "Keyword search" };
        var fromOption = new Option<DateTimeOffset?>("--from") { Description = "Start date (ISO 8601)" };
        var toOption = new Option<DateTimeOffset?>("--to") { Description = "End date (ISO 8601)" };
        var pageOption = new Option<int>("--page") { Description = "Page number" };
        pageOption.DefaultValueFactory = _ => 1;
        var pageSizeOption = new Option<int>("--page-size") { Description = "Page size" };
        pageSizeOption.DefaultValueFactory = _ => 50;
        var instanceNameOption = new Option<string?>("--instance-name") { Description = "Filter by instance name" };

        var cmd = new Command("event-log") { Description = "Query site event logs" };
        cmd.Add(siteOption);
        cmd.Add(eventTypeOption);
        cmd.Add(severityOption);
        cmd.Add(keywordOption);
        cmd.Add(fromOption);
        cmd.Add(toOption);
        cmd.Add(pageOption);
        cmd.Add(pageSizeOption);
        cmd.Add(instanceNameOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new QueryEventLogsCommand(
                    result.GetValue(siteOption)!,
                    result.GetValue(eventTypeOption),
                    result.GetValue(severityOption),
                    result.GetValue(keywordOption),
                    result.GetValue(fromOption),
                    result.GetValue(toOption),
                    result.GetValue(pageOption),
                    result.GetValue(pageSizeOption),
                    result.GetValue(instanceNameOption)));
        });
        return cmd;
    }

    private static Command BuildParkedMessages(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
    {
        var siteOption = new Option<string>("--site") { Description = "Site identifier", Required = true };
        var pageOption = new Option<int>("--page") { Description = "Page number" };
        pageOption.DefaultValueFactory = _ => 1;
        var pageSizeOption = new Option<int>("--page-size") { Description = "Page size" };
        pageSizeOption.DefaultValueFactory = _ => 50;

        var cmd = new Command("parked-messages") { Description = "Query parked messages at a site" };
        cmd.Add(siteOption);
        cmd.Add(pageOption);
        cmd.Add(pageSizeOption);
        cmd.SetAction(async (ParseResult result) =>
        {
            return await CommandHelpers.ExecuteCommandAsync(
                result, urlOption, formatOption, usernameOption, passwordOption,
                new QueryParkedMessagesCommand(
                    result.GetValue(siteOption)!,
                    result.GetValue(pageOption),
                    result.GetValue(pageSizeOption)));
        });
        return cmd;
    }
}
