// C:\Users\adm\Source\Repos\presence1\Demo\Domain\UseCase\UseCaseGeneratePresence.cs
using System;
using System.Linq;
using Demo.Data.LocalData;
using Demo.Data.LocalData.Entity;
using Demo.Data.Repository;

namespace Demo.Domain.UseCase
{
    public class UseCaseGeneratePresence
    {
        private readonly IPresenceRepository _presenceRepository;
        private readonly IUserRepository _userRepository;

        public UseCaseGeneratePresence(IPresenceRepository presenceRepository, IUserRepository userRepository)
        {
            _presenceRepository = presenceRepository;
            _userRepository = userRepository;
        }

        public void GenerateDailyPresence(int groupId, int firstLesson, int lastLesson, DateTime date)
        {
            var users = _userRepository.GetAllUsers().Where(u => u.GroupID == groupId).ToList();

            foreach (var user in users)
            {
                for (int lesson = firstLesson; lesson <= lastLesson; lesson++)
                {
                    var existing = LocalStaticData.Presences.FirstOrDefault(p =>
                        p.UserId == user.Id &&
                        p.ClassDate.Date == date.Date &&
                        p.LessonNumber == lesson);

                    if (existing == null)
                    {
                        var presence = new LocalPresence
                        {
                            UserId = user.Id,
                            ClassDate = date.Date,
                            LessonNumber = lesson,
                            WasPresent = true
                        };
                        _presenceRepository.AddPresence(presence);
                    }
                }
            }
        }

        public void GenerateWeeklyPresence(int groupId, int firstLesson, int lastLesson, DateTime startDate)
        {
            for (int i = 0; i < 7; i++)
            {
                var date = startDate.AddDays(i);
                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                    continue; // Пропускаем выходные
                GenerateDailyPresence(groupId, firstLesson, lastLesson, date);
            }
        }
    }
}