using DomainResults.Common;

using DataProviders;
using DataProviders.Buckets;
using Core.Abstractions;

namespace WeatherForecast.Services {

  /// <summary>
  /// 
  /// </summary>
  public interface IFileService {

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <param name="file"></param>
    /// <returns></returns>
    (Guid?, IDomainResult) Post(Guid siteId, Guid userId, IFormFile file);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <param name="fileId"></param>
    /// <returns></returns>
    (BucketFile?, IDomainResult) Get(Guid siteId, Guid userId, Guid fileId);
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <param name="fileId"></param>
    /// <returns></returns>
    IDomainResult Delete(Guid siteId, Guid userId, Guid fileId);
  }

  /// <summary>
  /// 
  /// </summary>
  public class FileService : ServiceBase<FileService>, IFileService {

    private readonly IFilesService _filesService;
    private readonly IImageBucketDataProvider _imageBucketDataProvider;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    /// <param name="filesService"></param>
    /// <param name="imageBucketDataProvider"></param>
    public FileService(
      ILogger<FileService> logger,
      IFilesService filesService,
      IImageBucketDataProvider imageBucketDataProvider
    ) : base(logger) {
      _filesService = filesService;
      _imageBucketDataProvider = imageBucketDataProvider;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <param name="file"></param>
    /// <returns></returns>
    public (Guid?, IDomainResult) Post(Guid siteId, Guid userId, IFormFile file) {
      var (list, result) = _filesService.Post(siteId, userId, new List<IFormFile> { file });

      if (!result.IsSuccess || list == null)
        return (null, result);

      return (list.First(), result);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <param name="fileId"></param>
    /// <returns></returns>
    public (BucketFile?, IDomainResult) Get(Guid siteId, Guid userId, Guid fileId) {
      try {
        var (file, result) = _imageBucketDataProvider.Download(siteId, userId, fileId);
        if (!result.IsSuccess || file == null)
          return (null, result);

        return IDomainResult.Success(file);
      }
      catch (Exception ex) {
        _logger.LogError(ex, "Unhandled exception");
        return IDomainResult.Failed<BucketFile?>(ex.Message);
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <param name="fileId"></param>
    /// <returns></returns>
    public IDomainResult Delete(Guid siteId, Guid userId, Guid fileId) {
      try {
        return _imageBucketDataProvider.DeleteOne(siteId, userId, fileId);
      }
      catch (Exception ex) {
        _logger.LogError(ex, "Unhandled exception");
        return IDomainResult.Failed(ex.Message);
      }
    }
  }
}
