namespace Application.Common;

using System.Text.RegularExpressions;

/// <summary>
/// Shared validation patterns and constants for input validation.
/// Used across validators to ensure consistency and DRY principle.
/// </summary>
public static partial class ValidationPatterns
{
    #region Name Validation

    /// <summary>
    /// Minimum length for student/person names.
    /// </summary>
    public const int MinNameLength = 3;

    /// <summary>
    /// Maximum length for student/person names.
    /// </summary>
    public const int MaxNameLength = 100;

    /// <summary>
    /// Pattern for valid name characters.
    /// Allows: Unicode letters, spaces, hyphens, apostrophes, and periods.
    /// Examples: "Juan Pérez", "María O'Brien", "Jean-Pierre"
    /// </summary>
    [GeneratedRegex(@"^[\p{L}\s\-'\.]+$", RegexOptions.Compiled)]
    public static partial Regex SafeNamePattern();

    #endregion

    #region Email Validation

    /// <summary>
    /// Maximum length for email addresses (RFC 5321).
    /// </summary>
    public const int MaxEmailLength = 254;

    #endregion

    #region Security Patterns

    /// <summary>
    /// Pattern to detect potentially dangerous content (XSS, script injection).
    /// Matches: HTML tags, javascript: protocol, event handlers (onclick, onload, etc.)
    /// </summary>
    [GeneratedRegex(@"<[^>]*>|javascript:|on\w+=", RegexOptions.IgnoreCase | RegexOptions.Compiled)]
    public static partial Regex DangerousContentPattern();

    /// <summary>
    /// Checks if a string contains potentially dangerous content.
    /// </summary>
    /// <param name="value">The string to check.</param>
    /// <returns>True if the content is safe (no dangerous patterns found).</returns>
    public static bool IsSafeContent(string? value) =>
        string.IsNullOrEmpty(value) || !DangerousContentPattern().IsMatch(value);

    #endregion

    #region ID Validation

    /// <summary>
    /// Error message for invalid entity ID.
    /// </summary>
    public const string InvalidIdMessage = "ID must be greater than 0";

    #endregion
}
