using Lada.Framework.DTO;
using Lada.Framework.DTO.SmartTable;
using Lada.Framework.Infrastructures;
using Lada.Framework.Infrastructures.Helpers;
using Ladaer.BackEnd.Infrastructures.DTO.Items.Identity;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Itentity;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Users;
using Ladaer.BackEnd.Infrastructures.DTO.Results.Users;
using Ladaer.BackEnd.Infrastructures.Services.Identity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using static IdentityServer4.IdentityServerConstants;

namespace Ladaer.BackEnd.Controllers.Identity
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;
        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }


        [HttpPost("Search")]
        [Authorize(Roles = "Role.Search")]
        public async Task<IActionResult> Search(SmartTableParam param)
        {
            var result = await _roleService.SearchAsync(param);
            return Ok(result);
        }

        [HttpGet("GetRoles")]
        [Authorize(Roles = "Role.GetRoles")]
        public async Task<IActionResult> GetRoles()
        {
            var result = new List<RoleItem>();
            var currentUser = User.GetCurrentUser();
            
            if (currentUser == null)
            {
                return Ok(result);
            }

            result = await _roleService.GetRolesAsync(currentUser.UserId);
            return Ok(result);
        }

        [HttpGet("GetById")]
        [Authorize(Roles = "Role.GetById")]
        public async Task<IActionResult> GetById(int roleId)
        {
            var result = await _roleService.GetByIdAsync(roleId);
            return Ok(result);
        }

        [HttpPost("CreateOrUpdate")]
        [Authorize(Roles = "Role.CreateOrUpdate")]
        public async Task<IActionResult> CreateOrUpdate([FromBody]RoleModel model)
        {
            var result = new BaseResult();
            var currentUser = User.GetCurrentUser();

            if (currentUser == null)
            {
                result.Result = Result.Failed;
                result.Message = "Không xác d?nh du?c ngu?i dùng";
                return Ok(result);
            }
            model.UpdatedBy = currentUser.UserId;
            model.UpdatedByUserName = currentUser.UserName;
            result = await _roleService.CreateOrUpdateAsync(model);
            return Ok(result);
        }


        [HttpDelete("Delete")]
        [Authorize(Roles = "Role.Delete")]
        public async Task<IActionResult> Delete(int roleId)
        {
            var result = new BaseResult();
            result = await _roleService.DeleteAsync(roleId);
            return Ok(result);
        }

        #region SET ROLE
        [HttpPut("SetRole")]
        [Authorize(Roles = "Role.SetRole")]
        public async Task<IActionResult> SetRole(SetRoleModel model)
        {
            var result = new BaseResult();
            var currentUser = User.GetCurrentUser();
            if (currentUser == null)
            {
                result.Result = Result.Failed;
                result.Message = "Không xác d?nh du?c ngu?i dùng";
                return Ok(result);
            }
            model.UpdatedBy = currentUser.UserId;
            model.UpdatedByUserName = currentUser.UserName;


            if (model.Id <= 0)
            {
                result.Result = Result.Failed;
                result.Message = "Mã ngu?i dùng không h?p l?";
                return Ok(result);
            }

            result = await _roleService.SetRoleAsync(model);
            return Ok(result);
        }
        #endregion
    }
}
