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

namespace DigitalData.UserManager.API.Controllers
{
    [Authorize]
    public class GroupController : BaseAuthController<IGroupService, GroupCreateDto, GroupReadDto, GroupUpdateDto, Group>
    {
        public GroupController(ILogger<GroupController> logger, IGroupService service, IUserService userService) : base(logger, service, userService)
        {
        }

        [HttpPost("ByDir")]
        public async Task<IActionResult> CreateByDir(DirectoryGroupDto adGroup)
        {
            try
            {
                return await _service.CreateAsync(adGroup).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);
            }
        }
    }
}