using LSFE.Domain.Contracts;
using LSFE.Domain.Services.Security;
using LSFE.Infrastructure.Dto;
using LSFE.Infrastructure.Entities;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace LSFE.API.Controllers.Account
{
    [Security.AuthorizeRoles("Account")]
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        public readonly IAccountService _accountService;
        public readonly IRoleAuthorizationCache _authCache;
        public AccountController(IAccountService accountService,
            IRoleAuthorizationCache authCache)
        {
            _accountService = accountService;
            _authCache = authCache;
        }
        #region Post Put Delete
        [HttpPost("AddRole")]
        public async Task<IActionResult> AddRoleToUser(string userId, string roleName)
        {
            var response = await _accountService.AddUserToRoleAsync(userId, roleName);
            if (response.Success)
            {
                return Ok(response);
            }
            return BadRequest(response);
        }

        [HttpPost("AddClaim")]
        public async Task<IActionResult> AddClaimToUser(string userId, string claimType, string claimValue)
        {
            var claim = new Claim(claimType, claimValue);
            var response = await _accountService.AddClaimToUserAsync(userId, claim);
            if (response.Success)
            {
                return Ok(response);
            }
            return BadRequest(response);
        }

        [HttpPost("Register")]
        public async Task<IActionResult> Register([FromBody] RegisterUserDto dto)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var response = await _accountService.RegisterUserAsync(dto);
            if (response.Success)
            {
                return Ok(response);
            }
            return BadRequest(response);
        }
        [HttpPost("UpdateUser")]
        public async Task<IActionResult> UpdateUser([FromBody] UpdateUserDto dto)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var response = await _accountService.UpdateUserAsync(dto);
            if (response.Success)
            {
                return Ok(response);
            }
            return BadRequest(response);
        }
        [HttpPost("PostPutDepartment")]
        public async Task<IActionResult> PostPutDepartment([FromBody] Departments dto)
        {
            if (dto == null)
                return BadRequest("Invalid department data.");

            var response = await _accountService.PostPutDepartmentAsync(dto);

            if (response.Success)
            {
                return Ok(response);
            }
            return BadRequest(response);
        }
        #endregion
        #region Get
        [HttpGet("GetDepartment")]
        public async Task<IActionResult> GetDepartment()
        {
            var departments = await _accountService.GetDepartment();
            return Ok(new { data = departments });
        }

        [HttpGet("GetUsers")]
        public async Task<IActionResult> GetUsers([FromQuery] string company)
        {
            var user = await _accountService.GetUsers(company);
            return Ok(new { data = user });
        }
        [HttpGet("GetUserRole")]
        public async Task<IActionResult> GetUserRole()
        {
            var user = await _accountService.GetUserRole();
            return Ok(new { data = user });
        }
        [HttpGet("GetUserById/{userId}")]
        public async Task<IActionResult> GetUserById(string userId)
        {
            var user = await _accountService.GetUserById(userId);
            return Ok(new { data = user });
        }
        #endregion
    }
}
