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

namespace skydiveLogs_api.Infrastructure
{
    public class JumpTypeRepository : IJumpTypeRepository
    {
        #region Public Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="JumpTypeRepository"/> class.
        /// </summary>
        /// <param name="dataProvider">The data provider used for database access operations.</param>
        /// <remarks>
        /// This constructor initializes the repository with a reference to the data provider
        /// and sets up the collection for jump type data operations.
        /// The data provider manages the underlying LiteDatabase connection and provides
        /// typed collection accessors for different entity types.
        /// </remarks>
        public JumpTypeRepository(IDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
            _col = _dataProvider.CollOfJumpType;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Adds a new jump type to the database.
        /// </summary>
        /// <param name="newJumpType">The jump type 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 jump type into the database using LiteDB.
        /// The jump type 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(JumpType newJumpType)
        {
            int result;

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

            return result;
        }

        /// <summary>
        /// Retrieves all jump types from the database.
        /// </summary>
        /// <returns>An enumerable collection containing all jump type instances stored in the database.</returns>
        /// <remarks>
        /// Queries the jump type collection and retrieves all records,
        /// then converts the result to a List for enumeration.
        /// Returns an empty list if no jump types exist in the database.
        /// </remarks>
        public IEnumerable<JumpType> GetAll()
        {
            return _col.FindAll().ToList();
        }

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

        /// <summary>
        /// Gets the total count of jump types in the database.
        /// </summary>
        /// <returns>The total number of jump types 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 the count of all jump type records in the database.
        /// </remarks>
        /// <exception cref="NotImplementedException">Thrown when the method is called.</exception>
        public int GetCount()
        {
            throw new NotImplementedException();
        }

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

        #endregion Public Methods

        #region Private Fields

        /// <summary>
        /// The LiteDB collection for jump type records.
        /// </summary>
        private readonly ILiteCollection<JumpType> _col;

        /// <summary>
        /// The data provider used for database access operations.
        /// </summary>
        private readonly IDataProvider _dataProvider;

        #endregion Private Fields
    }
}
