using DigitalData.Core.DTO;
using DigitalData.UserManager.Application.Contracts;
using DigitalData.UserManager.Application.DTOs.GroupOfUser;
using DigitalData.UserManager.Domain.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace DigitalData.UserManager.API.Controllers
{
    [Authorize]
    public class GroupOfUserController : BaseAuthController<IGroupOfUserService, GroupOfUserCreateDto, GroupOfUserReadDto, GroupOfUserUpdateDto, GroupOfUser>
    {
        public GroupOfUserController(ILogger<GroupOfUserController> logger, IGroupOfUserService service, IUserService userService) : base(logger, service, userService)
        {
        }
        
        [HttpDelete]
        public async Task<IActionResult> Delete([FromQuery] int groupId, [FromQuery] int userId)
        {
            try
            {
                return await _service.DeleteAsyncByGroupUserId(groupId, userId).ThenAsync(Ok, IActionResult (m, n) =>
                {
                    _logger.LogNotice(n);
                    return StatusCode(StatusCodes.Status500InternalServerError);
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "{Message}", ex.Message);
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }

        [NonAction]
        public override Task<IActionResult> GetAll() => base.GetAll();

        [HttpGet]
        public async Task<IActionResult> GetAll([FromQuery]bool withUser = false, [FromQuery]bool withGroup = false, [FromQuery] string? username = null)
        {
            try
            {
                if (username is not null)
                    return await _service.ReadByUsernameAsync(username).ThenAsync(Ok, IActionResult (m, n) =>
                    {
                        _logger.LogNotice(n);
                        return NotFound();
                    });

                return await _service.ReadAllAsyncWith(withUser, withGroup).ThenAsync(Ok, IActionResult (m, n) =>
                {
                    _logger.LogNotice(n);
                    return NotFound();
                });
            }
            catch(Exception ex)
            {
                _logger.LogError(ex, "{Message}", ex.Message);
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }

        [HttpGet("Has")]
        public async Task<IActionResult> HasGroup([FromQuery] string username, [FromQuery] string groupname)
        {
            try
            {
                return await _service.HasGroup(username, groupname).ThenAsync(Ok, (m, n) =>
                {
                    _logger.LogNotice(n);
                    return StatusCode(StatusCodes.Status500InternalServerError);
                });
            }
            catch(Exception ex)
            {
                _logger.LogError(ex, "{Message}", ex.Message);
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }
    }
}