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

namespace skydiveLogs_api.DomainBusiness
{
    public class StatsForLastYearByJumpTypeService : IStatsForLastYearByJumpTypeService
    {
        #region Public Constructors

        public StatsForLastYearByJumpTypeService(IJumpService jumpService,
                                                 IIdentityService identityService,
                                                 IStatsForLastYearByJumpTypeRepository statsForLastYearByJumpTypeRepository)
        {
            _jumpService = jumpService;
            _identityService = identityService;
            _statsForLastYearByJumpTypeRepository = statsForLastYearByJumpTypeRepository;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Retrieves statistics for the last year grouped by jump type.
        /// </summary>
        /// <returns>A collection of StatsForLastYearByJumpType entities containing the statistics.</returns>
        public IEnumerable<StatsForLastYearByJumpType> GetStats()
        {
            var allStats = _statsForLastYearByJumpTypeRepository.GetAll(_identityService.ConnectedUser);
            if (!allStats.Any())
            {
                var allJumps = _jumpService.GetAllJumps();
                var results = new List<StatsForLastYearByJumpType>();

                if (allJumps.Any())
                {
                    var lastJump = allJumps.OrderByDescending(j => j.JumpDate).First();
                    var yearOfLastJump = lastJump.JumpDate.Year;

                    results = [.. allJumps.Where(j => j.JumpDate.Year == yearOfLastJump)
                                          .GroupBy(j => j.JumpType.Name,
                                                   j => j,
                                                   (groupby, jumps) => new StatsForLastYearByJumpType
                                                   {
                                                       JumpType = groupby.ToString(),
                                                       Nb = jumps.Count(),
                                                       User = _identityService.ConnectedUser
                                                   })];
                }

                _statsForLastYearByJumpTypeRepository.Add(results);
                return results;
            }

            return allStats;
        }

        /// <summary>
        /// Resets the last year jump type statistics.
        /// </summary>
        public void Reset()
        {
            _statsForLastYearByJumpTypeRepository.Delete(_identityService.ConnectedUser);
        }

        #endregion Public Methods


        #region Private Fields

        private readonly IIdentityService _identityService;
        private readonly IJumpService _jumpService;
        private readonly IStatsForLastYearByJumpTypeRepository _statsForLastYearByJumpTypeRepository;

        #endregion Private Fields
    }
}
