using System.Security.Cryptography;

namespace Nexus.Api.Helpers;

/// <summary>
/// Helper methods for password generation and name construction.
/// </summary>
public static class PasswordHelper
{
    /// <summary>
    /// Generates a cryptographically random temporary password (30 chars, URL-safe base64).
    /// </summary>
    public static string GenerateTemporaryPassword()
        => Convert.ToBase64String(RandomNumberGenerator.GetBytes(18))
            .TrimEnd('=')
            .Replace('+', '-')
            .Replace('/', '_');

    /// <summary>
    /// Builds a human-readable display name from an email address.
    /// </summary>
    public static string BuildOwnerDisplayName(string email)
    {
        var localPart = email.Split('@', 2)[0].Trim();
        if (string.IsNullOrWhiteSpace(localPart)) return "Owner";

        var words = localPart
            .Replace('.', ' ')
            .Replace('_', ' ')
            .Replace('-', ' ')
            .Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
            .Select(word => char.ToUpperInvariant(word[0]) + word[1..].ToLowerInvariant());

        var displayName = string.Join(' ', words);
        return string.IsNullOrWhiteSpace(displayName) ? "Owner" : displayName;
    }
}
