using Microsoft.AspNetCore.Mvc;
using Demo.Data.RemoteData.RemoteDataBase.DAO;
using Demo.Domain.UseCase;
using Demo.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class PresenceController : ControllerBase
    {
        private readonly UseCasePresence _presenceUseCase;
        private readonly UserUseCase _userUseCase;
        private readonly GroupUseCase _groupUseCase;

        public PresenceController(UseCasePresence presenceUseCase, UserUseCase userUseCase, GroupUseCase groupUseCase)
        {
            _presenceUseCase = presenceUseCase;
            _userUseCase = userUseCase;
            _groupUseCase = groupUseCase;
        }

        [HttpGet("getPresence")]
        public IActionResult GetPresence([FromQuery] DateTime? startDate, [FromQuery] DateTime? endDate, [FromQuery] int? groupId, [FromQuery] Guid? userId)
        {
            var presence = _presenceUseCase.PresenceRepository.GetAllPresence();

            if (startDate.HasValue)
            {
                presence = presence.Where(p => p.Date >= startDate.Value);
            }

            if (endDate.HasValue)
            {
                presence = presence.Where(p => p.Date <= endDate.Value);
            }

            if (groupId.HasValue)
            {
                presence = presence.Where(p => _userUseCase.GetAllUsers().Any(u => u.Id == p.UserId && u.GroupID == groupId.Value));
            }

            if (userId.HasValue)
            {
                presence = presence.Where(p => p.UserId == userId.Value);
            }

            var result = new List<PresenceInfo>();
            foreach (var p in presence)
            {
                var user = _userUseCase.GetUserById(p.UserId);
                var group = _groupUseCase.GetAllGroups().FirstOrDefault(g => g.Id == user.GroupID);
                result.Add(new PresenceInfo
                {
                    GroupName = group?.Name,
                    FIO = user.FIO,
                    LessonNumber = p.LessonNumber,
                    Date = p.Date,
                    IsAttendance = p.IsAttendance
                });
            }

            return Ok(result);
        }

        [HttpPost("fillPresence")]
        public IActionResult FillPresence([FromBody] List<Demo.Domain.Models.Presence> presenceList)
        {
            foreach (var presence in presenceList)
            {
                _presenceUseCase.PresenceRepository.AddPresence(presence);
            }
            return Ok();
        }

        [HttpDelete("clearPresence")]
        public IActionResult ClearPresence([FromQuery] int? groupId, [FromQuery] Guid? userId, [FromQuery] DateTime? startDate, [FromQuery] DateTime? endDate)
        {
            var presence = _presenceUseCase.PresenceRepository.GetAllPresence();

            if (groupId.HasValue)
            {
                presence = presence.Where(p => _userUseCase.GetAllUsers().Any(u => u.Id == p.UserId && u.GroupID == groupId.Value));
            }

            if (userId.HasValue)
            {
                presence = presence.Where(p => p.UserId == userId.Value);
            }

            if (startDate.HasValue)
            {
                presence = presence.Where(p => p.Date >= startDate.Value);
            }

            if (endDate.HasValue)
            {
                presence = presence.Where(p => p.Date <= endDate.Value);
            }

            foreach (var p in presence)
            {
                _presenceUseCase.PresenceRepository.DeletePresence(p.Id);
            }

            return Ok();
        }

        [HttpPut("updatePresence")]
        public IActionResult UpdatePresence([FromQuery] Guid userId, [FromQuery] int lessonNumber, [FromQuery] DateTime date)
        {
            _presenceUseCase.MarkUserAsAbsent(userId, lessonNumber, date);
            return Ok();
        }
    }

    public class PresenceInfo
    {
        public string GroupName { get; set; }
        public string FIO { get; set; }
        public int LessonNumber { get; set; }
        public DateTime Date { get; set; }
        public bool IsAttendance { get; set; }
    }
}