namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier;

/// <summary>Outcome of parsing the command line: success carries the payload, failure carries a human reason.</summary>
internal sealed record ParseResult(bool Ok, RecipeDownload? Payload, string? Error)
{
    /// <summary>Builds a successful parse result carrying the parsed payload.</summary>
    /// <param name="payload">The successfully parsed recipe download payload.</param>
    /// <returns>A <see cref="ParseResult"/> with <see cref="Ok"/> set to <c>true</c>.</returns>
    public static ParseResult Success(RecipeDownload payload) => new(true, payload, null);

    /// <summary>Builds a failed parse result carrying a human-readable reason.</summary>
    /// <param name="error">The human-readable failure reason.</param>
    /// <returns>A <see cref="ParseResult"/> with <see cref="Ok"/> set to <c>false</c>.</returns>
    public static ParseResult Fail(string error) => new(false, null, error);
}

/// <summary>
/// Hand-rolled, reflection-free parser for the legacy WWNotifier flags. Each flag takes one value
/// (short or long form). The four required flags must be present; the two optional flags may be omitted.
/// </summary>
internal static class ArgParser
{
    /// <summary>Parses the legacy WWNotifier command-line flags into a <see cref="RecipeDownload"/> payload.</summary>
    /// <param name="args">The raw command-line arguments.</param>
    /// <returns>A successful <see cref="ParseResult"/> with the parsed payload, or a failed one with the reason.</returns>
    public static ParseResult Parse(string[] args)
    {
        var payload = new RecipeDownload();

        for (var i = 0; i < args.Length; i++)
        {
            var flag = args[i];
            if (i + 1 >= args.Length)
            {
                return ParseResult.Fail($"missing value for flag '{flag}'");
            }

            var value = args[++i];
            switch (flag)
            {
                case "-m" or "--machine":
                    payload.MachineCode = value;
                    break;
                case "-d" or "--downloadpath":
                    payload.DownloadPath = value;
                    break;
                case "-w" or "--workorder":
                    payload.WorkOrderNumber = value;
                    break;
                case "-p" or "--partnumber":
                    payload.PartNumber = value;
                    break;
                case "-s" or "--seqop":
                    payload.JobStepNumber = value;
                    break;
                case "-u" or "--username":
                    payload.Username = value;
                    break;
                default:
                    return ParseResult.Fail($"unknown flag '{flag}'");
            }
        }

        if (string.IsNullOrEmpty(payload.MachineCode))
        {
            return ParseResult.Fail("missing required flag -m/--machine");
        }
        if (string.IsNullOrEmpty(payload.DownloadPath))
        {
            return ParseResult.Fail("missing required flag -d/--downloadpath");
        }
        if (string.IsNullOrEmpty(payload.WorkOrderNumber))
        {
            return ParseResult.Fail("missing required flag -w/--workorder");
        }
        if (string.IsNullOrEmpty(payload.PartNumber))
        {
            return ParseResult.Fail("missing required flag -p/--partnumber");
        }

        return ParseResult.Success(payload);
    }
}
