using Spectre.Console;

namespace StellaOps.Cli.Prompts;

internal static class TrivyDbExportPrompt
{
    public static (bool? publishFull, bool? publishDelta, bool? includeFull, bool? includeDelta) PromptOverrides()
    {
        if (!AnsiConsole.Profile.Capabilities.Interactive)
        {
            return (null, null, null, null);
        }

        AnsiConsole.Write(
            new Panel("[bold]Trivy DB Export Overrides[/]")
                .Border(BoxBorder.Rounded)
                .Header("Trivy DB")
                .Collapse());

        var shouldOverride = AnsiConsole.Prompt(
            new SelectionPrompt<string>()
                .Title("Adjust publishing or offline bundle behaviour?")
                .AddChoices("Leave defaults", "Override"));

        if (shouldOverride == "Leave defaults")
        {
            return (null, null, null, null);
        }

        var publishFull = PromptBoolean("Push full exports to ORAS?");
        var publishDelta = PromptBoolean("Push delta exports to ORAS?");
        var includeFull = PromptBoolean("Include full exports in offline bundle?");
        var includeDelta = PromptBoolean("Include delta exports in offline bundle?");

        return (publishFull, publishDelta, includeFull, includeDelta);
    }

    private static bool? PromptBoolean(string question)
    {
        var choice = AnsiConsole.Prompt(
            new SelectionPrompt<string>()
                .Title($"{question} [grey](select override or keep default)[/]")
                .AddChoices("Keep default", "Yes", "No"));

        return choice switch
        {
            "Yes" => true,
            "No" => false,
            _ => (bool?)null,
        };
    }
}
