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;
    public AdminController(GroupUseCase groupUseCase, UserUseCase userUseCase, PresenceUseCase presenceUseCase){
    _groupUseCase = groupUseCase;
    _userUseCase = userUseCase;
    _presenceUseCase = presenceUseCase;
   }

    //post
    [HttpPost]
    public ActionResult<bool> CreateGroup([FromBody] GroupWithUsers request)
    {
        if (request == null || string.IsNullOrEmpty(request.GroupName))
        {
            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){
            return BadRequest("Invalid request");
        }
        bool isDeleted = _userUseCase.RemoveUserByGuid(userGuid);
        if (isDeleted == false){
            return NotFound("User not found");
        }
        return Ok(true);
    }

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

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

    [HttpDelete("groups")]
    public ActionResult<bool> DeleteGroups([FromBody] DeleteGroupsRequest request){
        if (request == null){
            return BadRequest("Invalid request");
        }
        foreach (int GroupID in request.GroupIDs)
        {
            bool isDeleted = _groupUseCase.RemoveGroupByID(GroupID);
            if (isDeleted == false){
                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){
            return Ok(user);
        } else{
            return NotFound("User not found");
        }
    }
}