using DomainResults.Common;
using DataProviders.Collections;
using WeatherForecast.Models.Responses;
using Core.Abstractions;
namespace WeatherForecast.Services {
  /// 
  /// 
  /// 
  public interface IContentService {
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    (ContentResponseModel?, IDomainResult) GetContent(Guid siteId, string locale);
  }
  /// 
  /// 
  /// 
  public class ContentService : ServiceBase, IContentService {
    private readonly IContentDataProvider _contentDataProvider;
    /// 
    /// 
    /// 
    /// 
    /// 
    public ContentService(
      ILogger logger,
      IContentDataProvider contentDataprovider
    ) : base(logger) {
      _contentDataProvider = contentDataprovider;
    }
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public (ContentResponseModel?, IDomainResult) GetContent(Guid siteId, string locale) {
      try {
        var (content, result) = _contentDataProvider.Get(siteId);
        if (!result.IsSuccess || content == null)
          return (null, result);
        return IDomainResult.Success(new ContentResponseModel(content.Single(x => x.Localization.Locale == locale)));
      }
      catch (Exception ex) {
        _logger.LogError(ex, "Unhandled exception");
        return IDomainResult.Failed(ex.Message);
      }
    }
  }
}