using System;
using System.Collections.Generic;
using System.Linq;
using Demo.Data.LocalData;
using Demo.Data.LocalData.Entity;

namespace Demo.Data.Repository
{
    public class AttendanceRepositoryImpl
    {
        private List<Attendance> _attendances;

        public AttendanceRepositoryImpl()
        {
            _attendances = new List<Attendance>();
        }

        public List<Attendance> GetAllAttendances()
        {
            return _attendances;
        }

        public Attendance GetAttendanceByUserAndDate(Guid userGuid, DateOnly date)
        {
            return _attendances.FirstOrDefault(a => a.UserGuid == userGuid && a.Date == date);
        }

        public void AddAttendance(Attendance attendance)
        {
            _attendances.Add(attendance);
        }

        public bool UpdateAttendance(Attendance attendance)
        {
            var existingAttendance = GetAttendanceByUserAndDate(attendance.UserGuid, attendance.Date);
            if (existingAttendance != null)
            {
                existingAttendance.IsPresent = attendance.IsPresent;
                return true;
            }
            return false;
        }

        public bool RemoveAttendance(Guid userGuid, DateOnly date)
        {
            var attendance = GetAttendanceByUserAndDate(userGuid, date);
            if (attendance != null)
            {
                _attendances.Remove(attendance);
                return true;
            }
            return false;
        }

        // Получение всех посещений по группе и дате
        public List<Attendance> GetAttendancesByGroupAndDate(int groupId, DateOnly date)
        {
            var userGuids = LocalStaticData.Users.Where(u => u.GroupID == groupId).Select(u => u.Guid).ToList();
            return _attendances.Where(a => userGuids.Contains(a.UserGuid) && a.Date == date).ToList();
        }

        // Получение всех посещений по группе и диапазону дат
        public List<Attendance> GetAttendancesByGroupAndDateRange(int groupId, DateOnly startDate, DateOnly endDate)
        {
            var userGuids = LocalStaticData.Users.Where(u => u.GroupID == groupId).Select(u => u.Guid).ToList();
            return _attendances.Where(a => userGuids.Contains(a.UserGuid) && a.Date >= startDate && a.Date <= endDate).ToList();
        }
    }
}