using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Knot.Contracts.Profiles.Domain;
using Knot.Shared.Kernel.Storage;

namespace Knot.Modules.Profiles.Infrastructure.Database;

internal class AvatarStorageService : IAvatarStorageService
{
    private readonly IFileStorageService _fileStorage;

    public AvatarStorageService(IFileStorageService fileStorage)
    {
        _fileStorage = fileStorage;
    }

    public async Task<string> UploadAsync(Stream stream, string fileName, string contentType, CancellationToken cancellationToken = default)
    {
        return await _fileStorage.UploadFileAsync(stream, fileName, contentType);
    }

    public async Task DeleteAsync(string fileKey, CancellationToken cancellationToken = default)
    {
        await _fileStorage.DeleteFileAsync(fileKey);
    }

    public async Task<Stream?> GetAsync(string fileKey, CancellationToken cancellationToken = default)
    {
        var (stream, _, _) = await _fileStorage.DownloadFileAsync(fileKey);
        return stream;
    }

    public string GetFileUrl(string fileKey)
    {
        return $"/api/files/{fileKey}";
    }
}
