using Data.RemoteData.DAO;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Zurnal.RemaDateBase.DateDao
{
    public class GroupInfo
    {
        public int TotalStudents { get; set; }
        public int TotalLessons { get; set; }
        public double AttendancePercentage { get; set; }
        public List<StudentAttendance> StudentAttendances { get; set; }
    }

    public class StudentAttendance
    {
        public string FIO { get; set; }
        public int AttendedLessons { get; set; }
        public int MissedLessons { get; set; }
        public double AttendanceRate { get; set; }
    }

    public class GroupService
    {
        public GroupInfo GetGroupInfo(GroupDao group)
        {
            var students = group.Users.ToList();
            var totalLessons = students.SelectMany(u => u.Group.Users.SelectMany(p => p.Presences)).Count();
            var totalAttendance = students.Sum(u => u.Presences.Count(p => p.IsAttendensy));
            var attendancePercentage = totalLessons > 0 ? (double)totalAttendance / totalLessons * 100 : 0;

            var studentAttendances = students.Select(u => new StudentAttendance
            {
                FIO = u.FIO,
                AttendedLessons = u.Presences.Count(p => p.IsAttendensy),
                MissedLessons = u.Presences.Count(p => !p.IsAttendensy),
                AttendanceRate = totalLessons > 0 ? (double)u.Presences.Count(p => p.IsAttendensy) / totalLessons * 100 : 0
            }).ToList();

            return new GroupInfo
            {
                TotalStudents = students.Count,
                TotalLessons = totalLessons,
                AttendancePercentage = attendancePercentage,
                StudentAttendances = studentAttendances
            };
        }
    }
}