using System.Text.RegularExpressions;

namespace BigRegister.Domain.Submissions;

/// <summary>
/// SERVER-OWNED submit rules (ported from the frontend submit-*.ts commands, where
/// they were hardcoded). Each method returns a rejection reason, or null when the
/// submission is accepted. The reference is generated server-side on acceptance.
/// </summary>
public static class SubmissionRules
{
  // RULE: a manually entered diploma cannot be auto-verified.
  public static string? RejectRegistratie(string diplomaHerkomst) =>
      diplomaHerkomst == "handmatig"
          ? "Een handmatig ingevoerd diploma kan niet automatisch worden geverifieerd. Uw aanvraag is doorgestuurd voor handmatige beoordeling."
          : null;

  // RULE: an application reporting zero worked hours is rejected.
  public static string? RejectZeroUren(int uren) =>
      uren == 0 ? "Aanvraag afgewezen: geen gewerkte uren geregistreerd." : null;

  private static readonly Regex PostcodePattern =
      new(@"^[1-9]\d{3}\s?[A-Z]{2}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);

  // RULE: a change request needs a street and a well-formed Dutch postcode. The
  // server re-validates format authoritatively (the FE check is UX-only).
  public static string? RejectChangeRequest(string straat, string postcode)
  {
    if (string.IsNullOrWhiteSpace(straat)) return "Vul straat en huisnummer in.";
    if (!PostcodePattern.IsMatch(postcode?.Trim() ?? "")) return "Voer een geldige postcode in, bijv. 1234 AB.";
    return null;
  }

  public static string NewReference() =>
      // ponytail: random reference is fine for a demo; a real system reserves it transactionally.
      "BIG-2026-" + Random.Shared.Next(100_000, 1_000_000);
}
