using DomainResults.Common;

using DataProviders.Collections;

using Core.Enumerations;
using Core.Abstractions;

using WeatherForecast.Models.Responses;

namespace WeatherForecast.Services {

  /// <summary>
  /// 
  /// </summary>
  public interface IShopCartItemsService {

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <param name="locale"></param>
    /// <returns></returns>
    (List<ShopCartItemResponseModel>?, IDomainResult) Get(Guid siteId, Guid userId, string? locale);
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <returns></returns>
    IDomainResult Delete(Guid siteId, Guid userId);
  }

  /// <summary>
  /// 
  /// </summary>
  public class ShopCartItemsService : ServiceBase<ShopCartItemsService>, IShopCartItemsService {

    private readonly IShopCatalogDataProvider _shopCatalogDataProvider;
    private readonly IShopCartDataProvider _shopCartDataProvider;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    /// <param name="shopCatalogDataProvider"></param>
    /// <param name="shopCartDataprovider"></param>
    public ShopCartItemsService(
      ILogger<ShopCartItemsService> logger,
      IShopCatalogDataProvider shopCatalogDataProvider,
      IShopCartDataProvider shopCartDataprovider
    ) : base(logger) {
      _shopCatalogDataProvider = shopCatalogDataProvider;
      _shopCartDataProvider = shopCartDataprovider;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="userId"></param>
    /// <param name="locale"></param>
    /// <returns></returns>
    public (List<ShopCartItemResponseModel>?, IDomainResult) Get(Guid siteId, Guid userId, string? locale) {
      try {
        var (cartItems, getCartItemsResult) = _shopCartDataProvider.GetAll(siteId, userId);
        if (!getCartItemsResult.IsSuccess || cartItems == null)
          return (null, getCartItemsResult);

        var items = new List<ShopCartItemResponseModel>();
        foreach (var cartItem in cartItems) {
          var (item, result) = _shopCatalogDataProvider.Get(siteId, cartItem.Sku);
          if (!result.IsSuccess || item == null) {
            var delteResult = _shopCartDataProvider.Delete(cartItem.Id);
            if (!delteResult.IsSuccess)
              return (null, delteResult);
          }
          else
            items.Add(new ShopCartItemResponseModel(item, cartItem, Enumeration.FromDisplayName<Locales>(locale ?? "en-US")));
        }

        return items.Count > 0
          ? IDomainResult.Success(items)
          : IDomainResult.NotFound<List<ShopCartItemResponseModel>?>();
      }
      catch (Exception ex) {
        _logger.LogError(ex, "Unhandled exception");
        return IDomainResult.Failed<List<ShopCartItemResponseModel>?>(ex.Message);
      }
    }

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