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

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

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

        [HttpGet("getAllUsers")]
        public IActionResult GetAllUsers()
        {
            var users = _userUseCase.GetAllUsers();
            return Ok(users);
        }

        [HttpPost("addGroup")]
        public IActionResult AddGroup([FromBody] GroupWithStudents groupWithStudents)
        {
            var group = new Group { Name = groupWithStudents.GroupName };
            _groupUseCase.AddGroup(group);

            foreach (var student in groupWithStudents.Students)
            {
                student.GroupID = group.Id;
                _userUseCase.UpdateUser(student);
            }

            return Ok();
        }

        [HttpDelete("deleteUser")]
        public IActionResult DeleteUser([FromQuery] Guid id)
        {
            _userUseCase.DeleteUser(id);
            return Ok();
        }

        [HttpDelete("deleteGroup")]
        public IActionResult DeleteGroup([FromQuery] int id)
        {
            _groupUseCase.DeleteGroup(id);
            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();
        }

        [HttpGet("getAllGroupsWithStudents")]
        public IActionResult GetAllGroupsWithStudents()
        {
            var groups = _groupUseCase.GetAllGroups();
            var users = _userUseCase.GetAllUsers();

            var result = groups.Select(g => new
            {
                Group = g,
                Students = users.Where(u => u.GroupID == g.Id).ToList()
            }).ToList();

            return Ok(result);
        }

        [HttpGet("getUserInfo")]
        public IActionResult GetUserInfo([FromQuery] Guid id)
        {
            var user = _userUseCase.GetUserById(id);
            if (user == null)
            {
                return NotFound();
            }

            var group = _groupUseCase.GetAllGroups().FirstOrDefault(g => g.Id == user.GroupID);
            var presence = _presenceUseCase.GetPresenceByGroup(user.Id);

            return Ok(new
            {
                User = user,
                Group = group,
                Presence = presence
            });
        }
    }

    public class GroupWithStudents
    {
        public string GroupName { get; set; }
        public List<User> Students { get; set; }
    }
}