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

namespace DigitalData.UserManager.API.Controllers
{
    [Authorize]
    public class UserController : BaseAuthController<IUserService, UserCreateDto, UserReadDto, UserUpdateDto, User>
    {
        public UserController(ILogger<UserController> logger, IUserService service) : base(logger, service, service)
        {
        }

        [HttpGet("ByModuleId/{moduleId}")]
        public async Task<IActionResult> GetByModuleId([FromRoute] int moduleId, [FromQuery]bool assigned = true)
        {
            try
            {
                return await (assigned ? _service.ReadByModuleIdAsync(moduleId) : _service.ReadUnassignedByModuleIdAsync(moduleId))
                    .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);
            }
        }

        [HttpGet("ByGroupId/{groupId}")]
        public async Task<IActionResult> GetByGroupId([FromRoute] int groupId, [FromQuery] bool assigned = true)
        {
            try
            {
                return await (assigned ? _service.ReadByGroupIdAsync(groupId) : _service.ReadUnassignedByGroupIdAsync(groupId))
                    .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);
            }
        }

        [HttpPost("ByDir")]
        public async Task<IActionResult> CreateByDir(UserPrincipalDto upDto)
        {
            try
            {
                return await _service.CreateAsync(upDto).ThenAsync(
                    Success: id =>
                    {
                        var createdResource = new { Id = id };
                        var actionName = nameof(GetById);
                        var routeValues = new { id = createdResource.Id };
                        return CreatedAtAction(actionName, routeValues, createdResource);
                    },
                    Fail: IActionResult (m, n) =>
                    {
                        _logger.LogNotice(n);
                        return BadRequest();
                    });
            }
            catch(Exception ex)
            {
                _logger.LogError(ex, "{Message}", ex.Message);
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }

        [HttpGet("ByUsername/{username}")]
        public virtual async Task<IActionResult> GetByUsername([FromRoute] string username)
        {
            try
            {
                return await _service.ReadByUsernameAsync(username).ThenAsync(Ok, IActionResult (m, n) =>
                {
                    _logger.LogNotice(n);
                    return NotFound();
                });
            }
            catch(Exception ex)
            {
                _logger.LogError(ex, "{Message}", ex.Message);
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }
    }
}