#nullable enable

namespace MBN_STOCK_WEBVIEW.DbWriteSmoke;

internal sealed record SmokeCommandOptions(
    bool ShowHelp,
    bool IsWriteAcknowledged,
    string? ConfigurationPath);

internal static class SmokeCommandLine
{
    internal const string WriteAcknowledgement =
        "--i-understand-this-writes-development-database";
    internal const string ConfigurationOption = "--configuration";

    internal static bool TryParse(
        IReadOnlyList<string> arguments,
        out SmokeCommandOptions options)
    {
        ArgumentNullException.ThrowIfNull(arguments);
        options = new SmokeCommandOptions(false, false, null);
        if (arguments.Count == 0)
        {
            return true;
        }

        if (arguments.Count == 1 &&
            arguments[0] is "--help" or "-h" or "/?")
        {
            options = options with { ShowHelp = true };
            return true;
        }

        var acknowledged = false;
        string? configurationPath = null;
        for (var index = 0; index < arguments.Count; index++)
        {
            var argument = arguments[index];
            switch (argument)
            {
                case WriteAcknowledgement when !acknowledged:
                    acknowledged = true;
                    break;
                case ConfigurationOption when configurationPath is null &&
                    index + 1 < arguments.Count:
                    configurationPath = arguments[++index];
                    if (string.IsNullOrWhiteSpace(configurationPath))
                    {
                        return false;
                    }

                    break;
                default:
                    return false;
            }
        }

        options = new SmokeCommandOptions(false, acknowledged, configurationPath);
        return true;
    }

    internal static string Usage =>
        "Usage: MBN_STOCK_WEBVIEW.DbWriteSmoke " + WriteAcknowledgement +
        " [" + ConfigurationOption + " <database.local.json>]";
}
