using System;
using System.Collections.Generic;
using System.Linq;
using Zurnal.RemaDateBase.DateDao;

public class GroupAttendanceService
{
    private List<PresnceDao> attendances;

    public GroupAttendanceService(List<PresnceDao> records)
    {
        attendances = records;
    }

    public void DisplayGroupInfo(Guid userGuid, object GroupName, IEnumerable<UserDao> users)
    {
        Console.WriteLine($"Группа: {GroupName}");
        Console.WriteLine($"Количество студентов: {users.Count()}");

        var totalClasses = new AttendanceCounter(attendances).CountAttendances(userGuid);
        Console.WriteLine($"Количество проведенных занятий: {totalClasses}");

        var overallAttendancePercentage = CalculateAttendancePercentage(attendances, userGuid);
        Console.WriteLine($"Общий процент посещаемости: {overallAttendancePercentage:F2}%");
        Console.WriteLine("Список студентов:");

        foreach (var user in users)
        {
            var attendanceInfo = GetAttendanceInfo(user);
            Console.WriteLine($"{user.FIO}: Посещенные занятия: {attendanceInfo.AttendedClasses}, Пропущенные занятия: {attendanceInfo.MissedClasses}, Процент посещаемости: {attendanceInfo.AttendancePercentage:F2}%");
        }
    }

    public class AttendanceCounter
    {
        private List<PresnceDao> _attendances;

        public AttendanceCounter(List<PresnceDao> attendances)
        {
            _attendances = attendances;
        }

        public int CountAttendances(Guid userGuid)
        {
            return _attendances.Count(a => a.UserGuid == userGuid && a.IsAttendensy);
        }
    }

    public static double CalculateAttendancePercentage(List<PresnceDao> attendanceRecords, Guid userGuid)
    {
        var userRecords = attendanceRecords.Where(record => record.UserGuid == userGuid).ToList();
        if (userRecords.Count == 0) return 0;

        int totalLessons = userRecords.Count;
        int attendedLessons = userRecords.Count(record => record.IsAttendensy);

        return (double)attendedLessons / totalLessons * 100;
    }

    private (int AttendedClasses, int MissedClasses, double AttendancePercentage) GetAttendanceInfo(UserDao user)
    {
        var attendanceRecords = this.attendances.Where(record => record.UserGuid == user.UserGuid).ToList(); 
        int attendedClasses = attendanceRecords.Count(record => record.IsAttendensy);
        int missedClasses = attendanceRecords.Count - attendedClasses; 
        double attendancePercentage = attendanceRecords.Count == 0 ? 0 : (double)attendedClasses / attendanceRecords.Count * 100; 

        return (attendedClasses, missedClasses, attendancePercentage);
    }

    public List<PresnceDao> GetAttendanceByUser(Guid userGuid)
    {
        return attendances.Where(record => record.UserGuid == userGuid).ToList();
    }

    public Dictionary<DateOnly, int> GetAttendanceSummary(Guid userGuid)
    {
        var userAttendance = attendances.Where(record => record.UserGuid == userGuid);
        var summary = new Dictionary<DateOnly, int>();

        foreach (var record in userAttendance)
        {
            if (summary.ContainsKey(record.Date))
            {
                summary[record.Date]++;
            }
            else
            {
                summary[record.Date] = 1;
            }
        }

        return summary; 
    }
}
