using Microsoft.Extensions.Logging;

using MongoDB.Driver;
using MongoDB.Bson.Serialization;

using DomainResults.Common;

using DataProviders.Abstractions;
using DomainObjects.Documents;
using DomainObjects.L10n;
using DomainObjects.Enumerations;

namespace DataProviders.Collections {

  public interface ICategoryDataProvider {
    (Guid?, IDomainResult) Insert(CategoryDocument obj);
    (Guid?, IDomainResult) CreateDefault(Guid siteId);
    (CategoryDocument?, IDomainResult) Get(Guid siteId, Guid categoryId);
    (List<CategoryDocument>?, IDomainResult) GetMany(Guid siteId, List<Guid> categoryIds);
    (CategoryDocument?, IDomainResult) GetBySlug(Guid siteId, string slug);
    (List<CategoryDocument>?, IDomainResult) GetBySlugs(Guid siteId, List<string> slugs);
    (List<CategoryDocument>?, IDomainResult) GetAll(Guid siteId);
    (Guid?, IDomainResult) Update(CategoryDocument obj);
    IDomainResult Delete(Guid id);
    IDomainResult DeleteAll(Guid siteId);
  }

  public class CategoryDataProvider : CollectionDataProviderBase<CategoryDocument>, ICategoryDataProvider {

    private const string _databaseName = "reactredux";
    private const string _collectionName = "categories";
    public CategoryDataProvider(
      ILogger<CollectionDataProviderBase<CategoryDocument>> logger,
      IMongoClient client,
      IIdGenerator idGenerator,
      ISessionService sessionService) : base(logger, client, idGenerator, sessionService, _databaseName, _collectionName) {
    }

    public (Guid?, IDomainResult) CreateDefault(Guid siteId) {
      var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.L10n.Any(y => y.Slug == "default"));

      if (result.IsSuccess && list != null)
        return (list.First().Id, result);

      return Insert(new CategoryDocument {
        SiteId = siteId,
        L10n = new List<CategoryL10n> {
          new CategoryL10n {
            Locale = Locales.Us,
            Slug = "default",
            Text = "Default category"
          }
        }
      });
    }

    public (CategoryDocument?, IDomainResult) Get(Guid siteId, Guid categoryId) {
      var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.Id == categoryId);

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

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

    public (List<CategoryDocument>?, IDomainResult) GetMany(Guid siteId, List<Guid> categoryIds) =>
      GetWithPredicate(x => x.SiteId == siteId && categoryIds.Contains(x.Id));

    public (CategoryDocument?, IDomainResult) GetBySlug(Guid siteId, string slug) {
      var (list, result) = GetBySlugs(siteId, new List<string> { slug });

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

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

    public (List<CategoryDocument>?, IDomainResult) GetBySlugs(Guid siteId, List<string> slugs) =>
      GetWithPredicate(x => x.SiteId == siteId && x.L10n.Any(y => slugs.Contains(y.Slug)));

    public (List<CategoryDocument>?, IDomainResult) GetAll(Guid siteId) =>
      GetWithPredicate(x => x.SiteId == siteId);

    public (Guid?, IDomainResult) Update(CategoryDocument obj) =>
      UpdateWithPredicate(obj, x => x.Id == obj.Id);

    public IDomainResult Delete(Guid id) =>
      DeleteWithPredicate(x => x.Id == id);

    public IDomainResult DeleteAll(Guid siteId) =>
      DeleteManyWithPredicate(x => x.SiteId == siteId);
  }
}
