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

namespace skydiveLogs_api.Infrastructure
{
    public class UserImageRepository : IUserImageRepository
    {
        #region Public Constructors

        public UserImageRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfImage;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Adds a new user image to the database.
        /// </summary>
        /// <param name="newImage">The user image instance to insert into the database.</param>
        /// <returns>The number of rows affected. Returns 0 if the insert operation failed.</returns>
        /// <remarks>
        /// Attempts to insert the new user image into the database using LiteDB.
        /// The user image should have all required properties set before insertion.
        /// If an exception occurs during insertion, the method returns 0.
        /// The returned value indicates the number of records affected by the insert operation.
        /// </remarks>
        public int Add(UserImage newImage)
        {
            int result;

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

            return result;
        }

        /// <summary>
        /// Retrieves all user images from the database.
        /// </summary>
        /// <returns>An enumerable collection containing all user image instances stored in the database.</returns>
        /// <remarks>
        /// This method is not currently implemented and throws a <see cref="NotImplementedException"/> when called.
        /// Implement this method to retrieve all user image records from the database.
        /// </remarks>
        /// <exception cref="NotImplementedException">Thrown when the method is called.</exception>
        public IEnumerable<UserImage> GetAll()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Retrieves all user images for a specific user.
        /// </summary>
        /// <param name="user">The user whose image records to retrieve.</param>
        /// <returns>An enumerable collection containing all user image instances belonging to the specified user.</returns>
        /// <remarks>
        /// Queries the user image collection and retrieves all records where the user ID matches the provided user.
        /// Each returned image record includes navigation properties to the associated user entity.
        /// Returns an empty collection if the user has no profile images.
        /// </remarks>
        public IEnumerable<UserImage> GetAll(User user)
        {
            return _col.Include(x => x.User)
                       .Query()
                       .Where(j => j.User.Id == user.Id)
                       .ToList();
        }

        /// <summary>
        /// Retrieves a user image by its unique identifier.
        /// </summary>
        /// <param name="id">The unique identifier of the user image to retrieve.</param>
        /// <returns>The user image instance if found, otherwise null.</returns>
        /// <remarks>
        /// Searches the user image collection for a record with the specified ID.
        /// Returns null if no user image with the matching ID is found in the database.
        /// </remarks>
        public UserImage GetById(int id)
        {
            return _col.FindById(new BsonValue(id));
        }

        /// <summary>
        /// Gets the total count of user images for a specific user.
        /// </summary>
        /// <param name="user">The user whose image records to count.</param>
        /// <returns>The total number of user image records for the specified user.</returns>
        /// <remarks>
        /// This method is not currently implemented and throws a <see cref="NotImplementedException"/> when called.
        /// Implement this method to retrieve the count of user image records per user.
        /// </remarks>
        /// <exception cref="NotImplementedException">Thrown when the method is called.</exception>
        public int GetCount()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Updates an existing user image record in the database.
        /// </summary>
        /// <param name="image">The user image instance containing the updated data.</param>
        /// <returns><see langword="true"/> if the update was successful; otherwise, <see langword="false"/>.</returns>
        /// <remarks>
        /// Updates the user image record in the database with the provided image instance.
        /// The user image must have a valid ID to be updated.
        /// The method returns the result of the underlying LiteDB update operation.
        /// If the user image does not exist, the update operation will return 0.
        /// </remarks>
        public bool Update(UserImage image)
        {
            return _col.Update(image);
        }

        #endregion Public Methods

        #region Private Fields

        private readonly ILiteCollection<UserImage> _col;

        private readonly IDataProvider _dataProvider;

        #endregion Private Fields
    }
}
