using Core.DomainObjects;
using DataProviders.Abstractions;
using DomainResults.Common;
using Microsoft.Extensions.Logging;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataProviders.Collections {

  public interface IUserDataProvider {
    (User?, IDomainResult) Get(Guid userId);

    (User?, IDomainResult) GetByNickName(string nickName);
  }

  public class UserDataProvider : CollectionDataProviderBase<User>, IUserDataProvider {

    private const string _collectionName = "users";

    public UserDataProvider(
      ILogger<UserDataProvider> logger,
      IMongoClient client,
      IIdGenerator idGenerator,
      ISessionService sessionService) : base(logger, client, idGenerator, sessionService) {
    }

    public (User?, IDomainResult) Get( Guid userId) {
      var (list, result) = GetWithPredicate(x => x.Id == userId, _collectionName);

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

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

    public (User?, IDomainResult) GetByNickName(string nickName) {
      var (list, result) = GetWithPredicate(x => x.NickName == nickName, _collectionName);

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

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