using Core.Abstractions;
using DataProviders.Collections;
using DomainResults.Common;

namespace WeatherForecast.Services.Abstractions {

  /// <summary>
  /// 
  /// </summary>
  public abstract class PostItemServiceBase<T> : ServiceBase<T> {

    /// <summary>
    /// 
    /// </summary>
    protected readonly ICategoryDataProvider _categoryDataProvider;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="logger"></param>
    /// <param name="categoryDataProvider"></param>
    public PostItemServiceBase(
      ILogger<T> logger,
      ICategoryDataProvider categoryDataProvider
    ) : base(logger) {
      _categoryDataProvider = categoryDataProvider;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="siteId"></param>
    /// <param name="categories"></param>
    /// <returns></returns>
    public (List<Guid>?, IDomainResult) AddCategoryIfNullOrEmpty(Guid siteId, List<Guid>? categories) {
      categories ??= new List<Guid>();

      if (categories.Count == 0) {
        var (catId, catCreateResult) = _categoryDataProvider.CreateDefault(siteId);
        if (!catCreateResult.IsSuccess || catId == null)
          return IDomainResult.Failed<List<Guid>?>();

        categories.Add(catId.Value);
      }

      return IDomainResult.Success(categories);
    }
  }
}
