using Demo.Domain.Models;
using Demo.Domain.UseCase;
using Microsoft.AspNetCore.Mvc;
using Npgsql.TypeMapping;

namespace presence_api.Controllers;
[ApiController]
[Route("api/[controller]")]

public class AdminController: ControllerBase{
    private readonly GroupUseCase _groupUseCase;
    private readonly UserUseCase _userUseCase;
    private readonly PresenceUseCase _presenceUseCase;
    private readonly ILogger<AdminController> _logger;
    public AdminController(GroupUseCase groupUseCase, UserUseCase userUseCase, PresenceUseCase presenceUseCase, ILogger<AdminController> logger){
    _groupUseCase = groupUseCase;
    _userUseCase = userUseCase;
    _presenceUseCase = presenceUseCase;
    _logger = logger;
   }

    //post
    [HttpPost]
    public ActionResult<bool> CreateGroup([FromBody] GroupWithUsers request)
    {
        if (request == null || string.IsNullOrEmpty(request.GroupName))
        {
            _logger.LogWarning("CreateGroup: Invalid request");
            return BadRequest("Invalid request");
        }

        bool isCreated = _groupUseCase.CreateGroup(request.GroupName);


        foreach(var user in request.Users){
            var usert = new User{FIO = user.FIO, Guid = Guid.NewGuid(), Group = user.Group};
            _userUseCase.CreateUser(usert);
        }
        return Ok(isCreated);
    }

    //delete
    [HttpDelete("user")]
    public ActionResult<bool> DeleteUser(Guid userGuid){
        if (userGuid == Guid.Empty){
            _logger.LogWarning("DeleteUser: Invalid Guid or empty");
            return BadRequest("Invalid request");
        }
        bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
        if (isDeleted == false){
            _logger.LogWarning("DeleteUser: User with Guid '{UserGuid}' not found", userGuid);
            return NotFound("User not found");
        }
        return Ok(true);
    }

    [HttpDelete("users")]
    public ActionResult<bool> DeleteUsers([FromBody] DeleteUsersRequest request){
        if (request == null){
            _logger.LogWarning("DeleteUsers: Invalid request");
            return BadRequest("Invalid request");
        }
        foreach (Guid userGuid in request.UsersGuid)
        {
            bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
            if (isDeleted == false){
                _logger.LogWarning("DeleteUsers: User with Guid '{UserGuid}' not found", userGuid);
                return NotFound("User not found");
            }
        }
        return Ok(true);
    }

    [HttpDelete("group")]
    public ActionResult<bool> DeleteGroup(int GroupID){
        bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
        if (isDeleted == false){
            _logger.LogWarning("DeleteGroup: Group with GroupID '{GroupID}' not found", GroupID);
            return NotFound("Group not found");
        }
        return Ok(true);
    }

    [HttpDelete("groups")]
    public ActionResult<bool> DeleteGroups([FromBody] DeleteGroupsRequest request){
        if (request == null){
            _logger.LogWarning("DeleteGroups: Invalid request");
            return BadRequest("Invalid request");
        }
        foreach (int GroupID in request.GroupIDs)
        {
            bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
            if (isDeleted == false){
                _logger.LogWarning("DeleteGroup: Group with GroupID '{GroupID}' not found", GroupID);
                return NotFound("Group not found");
            }
        }
        return Ok(true);
    }

    [HttpDelete("presence")]
    public ActionResult<bool> DeletePresence(){
        return Ok(_presenceUseCase.DeletePresence());
    }

    //get
    [HttpGet]
    public ActionResult<IEnumerable<GroupCon>> getGroupsWithUsers()
    {
        return Ok(_groupUseCase.GetAllGroupsWithUsers());
    }

    [HttpGet("user/{userGuid}")]
    public ActionResult<User> GetUserByGuid(Guid userGuid)
    {
        var user = _userUseCase.GetUserByGuid(userGuid);
        if (user != null){
            _logger.LogWarning("found");
            return Ok(user);
        } else{
            _logger.LogWarning("GetUserByGuid: User with Guid '{UserGuid}' not found", userGuid);
            return NotFound("User not found");
        }
    }
}