using DomainResults.Common;

using Core.Enumerations;
using ExtensionMethods;
using DataProviders.Collections;

using WeatherForecast.Models.Requests;
using WeatherForecast.Models.Responses;
using WeatherForecast.Services.Abstractions;

namespace WeatherForecast.Services {

  /// <summary>
  /// 
  /// </summary>
  public interface IBlogItemService {
    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="requestModel"></param>
    /// <returns></returns>
    (Guid?, IDomainResult) Post(Guid siteId, BlogItemRequestModel requestModel);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="blogId"></param>
    /// <returns></returns>
    (BlogItemResponseModel?, IDomainResult) Get(Guid siteId, Guid blogId);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="slug"></param>
    /// <returns></returns>
    (BlogItemResponseModel?, IDomainResult) GetSlug(Guid siteId, string slug);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="blogId"></param>
    /// <param name="requestData"></param>
    /// <returns></returns>
    (Guid?, IDomainResult) Update(Guid siteId, Guid blogId, BlogItemRequestModel requestData);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="blogId"></param>
    /// <returns></returns>
    IDomainResult Delete(Guid siteId, Guid blogId);
  }

  /// <summary>
  /// 
  /// </summary>
  public class BlogItemService : PostItemServiceBase<BlogItemService>, IBlogItemService {

    private readonly IBlogCatalogDataProvider _blogCatalogDataProvider;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    /// <param name="blogCatalogDataProvider"></param>
    /// <param name="categoryDataProvider"></param>
    public BlogItemService(
      ILogger<BlogItemService> logger,
      IBlogCatalogDataProvider blogCatalogDataProvider,
      ICategoryDataProvider categoryDataProvider
    ) : base(logger, categoryDataProvider) {
      _blogCatalogDataProvider = blogCatalogDataProvider;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="requestData"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public (Guid?, IDomainResult) Post(Guid siteId, BlogItemRequestModel requestData) {
      try {
        var item = requestData.ToDomainObject();
        item.SiteId = siteId;

        var (_, getResult) = _blogCatalogDataProvider.GetBySlugs(item.SiteId, item.L10n.Select(x => x.Slug).ToList());
        if (getResult.IsSuccess)
          return IDomainResult.Failed<Guid?>();

        // TODO: should be recovered from users by jwt
        item.Author = "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60".ToGuid();

        var (categories, addCategoriesResult) = AddCategoryIfNullOrEmpty(siteId, requestData.Categories);
        if (!addCategoriesResult.IsSuccess || categories == null)
          return (null, addCategoriesResult);

        item.Categories = categories;

        var (id, insertResult) = _blogCatalogDataProvider.Insert(item);
        if (!insertResult.IsSuccess)
          return IDomainResult.Failed<Guid?>();

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

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="blogId"></param>
    /// <returns></returns>
    public (BlogItemResponseModel?, IDomainResult) Get(Guid siteId, Guid blogId) {
      try {
        var (item, result) = _blogCatalogDataProvider.Get(siteId, blogId);

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

        var (categories, getCategoryResult) = _categoryDataProvider.GetMany(siteId, item.Categories);
        if (!getCategoryResult.IsSuccess || categories == null)
          return IDomainResult.Failed<BlogItemResponseModel?>();

        return IDomainResult.Success(new BlogItemResponseModel(item, categories));
      }
      catch (Exception ex) {
        _logger.LogError(ex, "Unhandled exception");
        return IDomainResult.Failed<BlogItemResponseModel?>(ex.Message);
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="slug"></param>
    /// <returns></returns>
    public (BlogItemResponseModel?, IDomainResult) GetSlug(Guid siteId, string slug) {
      try {
        var (item, result) = _blogCatalogDataProvider.GetBySlug(siteId, slug);

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

        var locale = item.L10n.SingleOrDefault(x => x.Slug == slug)?.Locale ?? Locales.Us;

        var (categories, getCategoryResult) = _categoryDataProvider.GetMany(siteId, item.Categories);
        if (!getCategoryResult.IsSuccess || categories == null)
          return IDomainResult.Failed<BlogItemResponseModel?>();

        return IDomainResult.Success(new BlogItemResponseModel(item, categories, locale));
      }
      catch (Exception ex) {
        _logger.LogError(ex, "Unhandled exception");
        return IDomainResult.Failed<BlogItemResponseModel>(ex.Message);
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="blogId"></param>
    /// <param name="requestData"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public (Guid?, IDomainResult) Update(Guid siteId, Guid blogId, BlogItemRequestModel requestData) {
      try {
        var (item, getResult) = _blogCatalogDataProvider.Get(siteId, blogId);
        if (!getResult.IsSuccess || item == null)
          return (null, getResult);

        // construct domain object from model
        var newItem = requestData.ToDomainObject();
        newItem.Id = item.Id;
        newItem.SiteId = siteId;

        newItem.Created = item.Created;
        newItem.Author = item.Author;

        var (categories, addCategoriesResult) = AddCategoryIfNullOrEmpty(siteId, requestData.Categories);
        if (!addCategoriesResult.IsSuccess || categories == null)
          return (null, addCategoriesResult);

        item.Categories = categories;

        if (!item.Equals(newItem)) {
          var (id, updateResult) = _blogCatalogDataProvider.Update(newItem);
          if (!updateResult.IsSuccess || id == null)
            return (null, updateResult);
        }

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

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="blogId"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public IDomainResult Delete(Guid siteId, Guid blogId) {
      try {
        var (item, getResult) = _blogCatalogDataProvider.Get(siteId, blogId);
        if (!getResult.IsSuccess || item == null)
          return getResult;

        var result = _blogCatalogDataProvider.Delete(item.Id);
        if (!result.IsSuccess)
          return result;

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