using Demo.Data.RemoteData.RemoteDataBase;
using Demo.Domain.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo.Data.Repository
{
    public class SQLPresenceRepository : IPresenceRepository
    {
        private readonly DbContext _context;

        public SQLPresenceRepository(DbContext context)
        {
            _context = context;
        }

        public void AddPresence(Presence presence)
        {
            presence.Date = DateTime.SpecifyKind(presence.Date, DateTimeKind.Utc);
            _context.Set<RemoteData.RemoteDataBase.DAO.Presence>().Add(new RemoteData.RemoteDataBase.DAO.Presence
            {
                Id = presence.Id,
                Date = presence.Date,
                LessonNumber = presence.LessonNumber,
                IsAttendance = presence.IsAttendance,
                UserId = presence.UserId
            });
            _context.SaveChanges();
        }

        public void AddPresence(RemoteData.RemoteDataBase.DAO.Presence presence)
        {
            presence.Date = DateTime.SpecifyKind(presence.Date, DateTimeKind.Utc);
            _context.Set<RemoteData.RemoteDataBase.DAO.Presence>().Add(presence);
            _context.SaveChanges();
        }

        public IEnumerable<Presence> GetPresenceByGroup(int groupId)
        {
            return _context.Set<RemoteData.RemoteDataBase.DAO.Presence>()
                .Where(p => _context.Set<RemoteData.RemoteDataBase.DAO.User>().Any(u => u.Id == p.UserId && u.GroupID == groupId))
                .Select(p => new Presence
                {
                    Id = p.Id,
                    Date = p.Date,
                    LessonNumber = p.LessonNumber,
                    IsAttendance = p.IsAttendance,
                    UserId = p.UserId
                })
                .ToList();
        }

        public IEnumerable<Presence> GetPresenceByGroupAndDate(int groupId, DateTime date)
        {
            date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
            return _context.Set<RemoteData.RemoteDataBase.DAO.Presence>()
                .Where(p => _context.Set<RemoteData.RemoteDataBase.DAO.User>().Any(u => u.Id == p.UserId && u.GroupID == groupId) && p.Date == date)
                .Select(p => new Presence
                {
                    Id = p.Id,
                    Date = p.Date,
                    LessonNumber = p.LessonNumber,
                    IsAttendance = p.IsAttendance,
                    UserId = p.UserId
                })
                .ToList();
        }

        public void MarkUserAsAbsent(Guid userId, int lessonNumber, DateTime date)
        {
            date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
            var presence = _context.Set<RemoteData.RemoteDataBase.DAO.Presence>()
                .FirstOrDefault(p => p.UserId == userId && p.LessonNumber == lessonNumber && p.Date == date);
            if (presence != null)
            {
                presence.IsAttendance = false;
                _context.SaveChanges();
            }
        }

        public IEnumerable<Presence> GetAllPresence()
        {
            return _context.Set<RemoteData.RemoteDataBase.DAO.Presence>()
                .Select(p => new Presence
                {
                    Id = p.Id,
                    Date = p.Date,
                    LessonNumber = p.LessonNumber,
                    IsAttendance = p.IsAttendance,
                    UserId = p.UserId
                })
                .ToList();
        }

        public void DeletePresence(Guid id)
        {
            var presence = _context.Set<RemoteData.RemoteDataBase.DAO.Presence>().Find(id);
            if (presence != null)
            {
                _context.Set<RemoteData.RemoteDataBase.DAO.Presence>().Remove(presence);
                _context.SaveChanges();
            }
        }

        public void UpdatePresence(Presence presence)
        {
            var existingPresence = _context.Set<RemoteData.RemoteDataBase.DAO.Presence>().Find(presence.Id);
            if (existingPresence != null)
            {
                existingPresence.Date = presence.Date;
                existingPresence.LessonNumber = presence.LessonNumber;
                existingPresence.IsAttendance = presence.IsAttendance;
                existingPresence.UserId = presence.UserId;
                _context.SaveChanges();
            }
        }
    }
}