using LiteDB;
using skydiveLogs_api.Domain;
using skydiveLogs_api.DomainService.Repositories;
using skydiveLogs_api.Infrastructure.Interfaces;
using System;
using System.Collections.Generic;

namespace skydiveLogs_api.Infrastructure
{
    public class UserRepository : IUserRepository
    {
        #region Public Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UserRepository"/> class
        /// </summary>
        /// <param name="dataProvider">The data provider to use for data access</param>
        public UserRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfUser;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Adds a new user to the database
        /// </summary>
        /// <param name="newUser">The user instance to add</param>
        /// <returns>The number of rows affected (0 if insert failed)</returns>
        public int Add(User newUser)
        {
            int result;

            try
            {
                var tmp = _col.Insert(newUser);
                result = tmp.AsInt32;
            }
            catch
            {
                result = 0;
            }

            return result;
        }

        /// <summary>
        /// Retrieves all users from the database
        /// </summary>
        /// <returns>A collection of all user instances</returns>
        public IEnumerable<User> GetAll()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Retrieves a user by its unique identifier
        /// </summary>
        /// <param name="id">The unique identifier of the user</param>
        /// <returns>The user instance or null if not found</returns>
        public User GetById(int id)
        {
            return _col.FindById(new BsonValue(id));
        }

        /// <summary>
        /// Retrieves a user by their login credentials
        /// </summary>
        /// <param name="login">The user's login</param>
        /// <param name="password">The user's password</param>
        /// <returns>The user instance or null if not found</returns>
        public User GetByLogin(string login, string password)
        {
            return _col.FindOne(u => u.Login == login && u.Password == password);
        }

        /// <summary>
        /// Gets the total count of users in the database
        /// </summary>
        /// <returns>The total number of users</returns>
        public int GetCount()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Updates an existing user in the database
        /// </summary>
        /// <param name="updated">The user instance to update</param>
        /// <returns>True if the update was successful, false otherwise</returns>
        public bool Update(User updated)
        {
            throw new NotImplementedException();
        }

        #endregion Public Methods

        #region Private Fields

        private readonly ILiteCollection<User> _col;
        private readonly IDataProvider _dataProvider;

        #endregion Private Fields
    }
}
