using IdentityServer4;
using Kimuser.Dto.Models.SystemUsers;
using Kimuser.Dto.SmartTable;
using Kimuser.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static IdentityServer4.IdentityServerConstants;

namespace Kimuser.Controllers
{
    /// <summary>
    /// SystemUserController
    /// </summary>
    [Authorize(LocalApi.PolicyName)]
    [Route("api/[controller]")]
    [ApiController]
    public class SystemUserController : ControllerBase
    {
        private readonly ISystemUserService _systemUserService;
        private readonly IRoleService _roleService;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="systemUserService"></param>
        public SystemUserController(ISystemUserService systemUserService, IRoleService roleService)
        {
            _systemUserService = systemUserService;
            _roleService = roleService;
        }

        /// <summary>
        /// Lấy thông tin System User
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet("GetSystemUser")]
        public async Task<IActionResult> GetSystemUser(int userId)
        {
            var result = await _systemUserService.GetUserAsync(userId);
            return Ok(result);
        }

        /// <summary>
        /// Tìm kiếm System User có phân trang
        /// </summary>
        /// <param name="param">Tham số dạng SmartTableParam</param>
        /// <returns></returns>
        [HttpPost("Search")]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var categories = await _systemUserService.Search(param);
            return Ok(categories);
        }


        



        /// <summary>
        /// Tạo / cập nhật System User
        /// </summary>
        /// <param name="postModel"></param>
        /// <returns></returns>
        [HttpPost("CreateOrUpdate")]
        public async Task<IActionResult> CreateOrUpdate([FromBody] SystemUserModel systemUserModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            //var currentUser = User.GetCurrentUser();
            //postModel.UpdatedBy = currentUser.UserId;
            //postModel.UpdatedByUserName = currentUser.UserName;
            //if (postModel.UpdatedBy <= 0 || string.IsNullOrEmpty(postModel.UpdatedByUserName))
            //{
            //    return BadRequest("Không xác định được người dùng");
            //}

            var result = await _systemUserService.CreateOrUpdate(systemUserModel);
            return Ok(result);
        }

        [HttpGet("GetRoles")]
        public async Task<IActionResult> GetRoles()
        {
            var result = _roleService.GetRoles();
            return Ok(result);
        }

        [HttpGet("GetRolesOfUser")]
        [AllowAnonymous]
        public async Task<IActionResult> GetRolesOfUser(int userId)
        {
            var result = await _systemUserService.GetRolesOfUser(userId);
            return Ok(result);
        }

        [HttpPost("CreateRole")]
        [AllowAnonymous]
        public async Task<IActionResult> CreateRole(string roleName)
        {
            var result = await _roleService.AddRole(roleName);
            return Ok(result);
        }

        [HttpPost("AddToRoles")]
        [AllowAnonymous]
        public async Task<IActionResult> AddToRole([FromBody] AddToRolesModel model)
        {
            var result = await _systemUserService.AddRoleToUserAsync(model.UserId, model.RoleNames);
            return Ok(result);
        }

        #region ChangePassword
        [HttpPost("ChangePassword")]
        public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordModel model)
        {
            var result = await _systemUserService.ChangePassword(model);
            return Ok(result);
        }
        #endregion



    }
}
