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 StatsForLastYearByDzService : IStatsForLastYearByDzService
    {
        #region Public Constructors

        public StatsForLastYearByDzService(IJumpService jumpService,
                                           IIdentityService identityService,
                                           IStatsForLastYearByDzRepository statsForLastYearByDzRepository)
        {
            _jumpService = jumpService;
            _identityService = identityService;
            _statsForLastYearByDzRepository = statsForLastYearByDzRepository;
        }

        #endregion Public Constructors

        #region Public Methods

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

                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.DropZone.Name,
                                                   j => j,
                                                   (groupby, jumps) => new StatsForLastYearByDz
                                                   {
                                                       DropZone = groupby.ToString(),
                                                       Nb = jumps.Count(),
                                                       User = _identityService.ConnectedUser
                                                   })];
                }

                _statsForLastYearByDzRepository.Add(results);
                return results;
            }

            return allStats;
        }

        /// <summary>
        /// Resets the last year drop zone statistics.
        /// </summary>
        public void Reset()
        {
            _statsForLastYearByDzRepository.Delete(_identityService.ConnectedUser);
        }

        #endregion Public Methods

        #region Private Fields

        private readonly IIdentityService _identityService;
        private readonly IJumpService _jumpService;
        private readonly IStatsForLastYearByDzRepository _statsForLastYearByDzRepository;

        #endregion Private Fields
    }
}
