using Microsoft.Extensions.Options;
using OmsorgCore.Application.Abstractions;

namespace OmsorgCore.Infrastructure.Storage;

public class DocumentUploadPolicy : IDocumentUploadPolicy
{
    private readonly StorageOptions _options;
    private readonly HashSet<string> _allowedContentTypes;

    public DocumentUploadPolicy(IOptions<StorageOptions> options)
    {
        _options = options.Value;
        _allowedContentTypes = _options.AllowedDocumentContentTypes
            .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
            .ToHashSet(StringComparer.OrdinalIgnoreCase);
    }

    public long MaxSizeBytes => _options.MaxDocumentSizeBytes;

    public bool IsSizeAllowed(long sizeBytes) => sizeBytes > 0 && sizeBytes <= MaxSizeBytes;

    public bool IsContentTypeAllowed(string? contentType)
        => !string.IsNullOrWhiteSpace(contentType) && _allowedContentTypes.Contains(contentType);
}
