using Health.Application.Reports;

namespace Health.Infrastructure.Reports;

public sealed class LocalReportFileStorage : IReportFileStorage
{
    public async Task<StoredReportFile> SaveAsync(ReportUploadFile file, string extension, CancellationToken ct)
    {
        var uploadsDir = Path.Combine(Directory.GetCurrentDirectory(), "uploads", "reports");
        Directory.CreateDirectory(uploadsDir);

        var fileName = $"{Guid.NewGuid()}{extension}";
        var filePath = Path.Combine(uploadsDir, fileName);
        await using (var stream = new FileStream(filePath, FileMode.CreateNew))
        {
            await file.Content.CopyToAsync(stream, ct);
        }

        return new StoredReportFile($"/uploads/reports/{fileName}", filePath);
    }

    public string GetLocalFilePath(string fileUrl) =>
        Path.Combine(Directory.GetCurrentDirectory(), fileUrl.TrimStart('/'));

    public bool Exists(string filePath) =>
        File.Exists(filePath);

    public void Delete(string filePath) =>
        File.Delete(filePath);
}
