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 StatsForLastMonthByJumpTypeService : IStatsForLastMonthByJumpTypeService
    {
        #region Public Constructors

        public StatsForLastMonthByJumpTypeService(IJumpService jumpService,
                                                  IIdentityService identityService,
                                                  IStatsForLastMonthByJumpTypeRepository statsForLastMonthByJumpTypeRepository)
        {
            _jumpService = jumpService;
            _identityService = identityService;
            _statsForLastMonthByJumpTypeRepository = statsForLastMonthByJumpTypeRepository;
        }

        #endregion Public Constructors

        #region Public Methods

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

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

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

                _statsForLastMonthByJumpTypeRepository.Add(results);
                return results;
            }

            return allStats;
        }

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

        #endregion Public Methods

        #region Private Fields

        private readonly IIdentityService _identityService;
        private readonly IJumpService _jumpService;
        private readonly IStatsForLastMonthByJumpTypeRepository _statsForLastMonthByJumpTypeRepository;

        #endregion Private Fields
    }
}
