#nullable enable
namespace Baya.Application.Common;

/// <summary>
/// Shared validation + storage-key rules for avatar (profile photo) uploads. Keeps the nurse and customer
/// upload handlers in lock-step on the accepted content types, the size cap, and the key scheme.
/// </summary>
public static class AvatarUpload
{
    public const long MaxSizeBytes = 5 * 1024 * 1024;

    private static readonly IReadOnlyDictionary<string, string> AllowedTypes = new Dictionary<string, string>
    {
        ["image/jpeg"] = ".jpg",
        ["image/png"] = ".png",
        ["image/webp"] = ".webp"
    };

    public static bool IsAllowedContentType(string? contentType)
        => contentType is not null && AllowedTypes.ContainsKey(contentType.ToLowerInvariant());

    public static bool IsWithinSizeLimit(long length) => length > 0 && length <= MaxSizeBytes;

    /// <summary>Opaque, collision-free storage key: <c>avatars/{scope}/{ownerId}/{token}{ext}</c>.</summary>
    public static string BuildKey(string scope, long ownerId, string contentType, string token)
    {
        var ext = AllowedTypes.TryGetValue(contentType.ToLowerInvariant(), out var e) ? e : ".bin";
        return $"avatars/{scope}/{ownerId}/{token}{ext}";
    }
}
