using Countries.NET.Database;
using Microsoft.AspNetCore.Authorization;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MTWorkHR.Application.Filters;
using MTWorkHR.Application.Identity;
using MTWorkHR.Application.Models;
using MTWorkHR.Identity.Services;

namespace MTWorkHR.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [AppAuthorize]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;
        public UserController(IUserService userService) 
        {
            this._userService = userService;
        }
        [HttpPost("GetAll")]
        [AppAuthorize(Permissions = "User")]
        public async Task<ActionResult<List<UserAllDto>>> GetAll([FromBody] UserPagingInputDto pagingInput)
        {
            return Ok( await _userService.GetAll(pagingInput));
        }

        [HttpGet("GetAllCompanyEmployees")]
        [AppAuthorize(Permissions = "User")]
        public async Task<ActionResult<List<UserDto>>> GetAllCompanyEmployees()
        {
            return Ok(await _userService.GetAllCompanyEmployees());
        }
        [HttpGet("Get")]
        [AppAuthorize(Permissions = "User")]
        public async Task<ActionResult<UserUpdateDto>> Get()
        {
            return Ok(await _userService.GetById());
        }
        [HttpGet("GetById")]
        [AppAuthorize(Permissions = "User")]
        public async Task<ActionResult<UserUpdateDto>> GetById([FromQuery] string userId)
        {
            return Ok(await _userService.GetByIdForUpdate(userId));
        }

        [HttpGet("GetEmployeeById")]
        [AppAuthorize(Permissions = "User")]
        public async Task<ActionResult<EmployeeInfoDto>> GetEmployeeById([FromQuery] string userId)
        {
            return Ok(await _userService.GetEmployeeInfo(userId));
        }

        [HttpGet("GetByEmail")]
        [AppAuthorize(Permissions = "User")]
        public async Task<ActionResult<UserDto>> GetByEmail(string userId)
        {
            return Ok(await _userService.GetByEmail(userId));
        }

        [HttpPost("Create")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [Consumes("multipart/form-data")]
        [AppAuthorize(Permissions = "User.Create")]
        public async Task<ActionResult<UserDto>> Create([FromForm] UserDto input)
        {
            return await _userService.Create(input);
        }

        [HttpPost("Update")]
     //   [Consumes("multipart/form-data")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "User.Update")]
        public async Task<ActionResult<UserDto>> Update([FromBody] UserUpdateDto input)
        {
            return Ok(await _userService.Update(input));
        }

        [Authorize(Policy = "SuperAdminOnly")]
        [HttpDelete("Delete")]
        [AppAuthorize(Permissions = "User.Delete")]
        [ProducesResponseType(StatusCodes.Status200OK)]

        public async Task Delete([FromQuery] string id)
        {
            await _userService.Delete(id);
        }

        [Authorize(Policy = "SuperAdminOnly")]
        [HttpDelete("Suspend")]
        [AppAuthorize(Permissions = "User.Suspend")]
        [ProducesResponseType(StatusCodes.Status200OK)]

        public async Task Suspend([FromQuery] string id)
        {
            await _userService.Suspend(id);
        }

        [HttpPost("ResetPassword")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "User.ResetPassword")]
        public async Task<bool> ResetPassword([FromBody] ResetPasswordDto input)
        {
            return await _userService.ResetPassword(input);
        }

        [HttpPost("SetUpJobInterview")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "User")]
        public async Task<ActionResult<JobInterviewDto>> SetUpJobInterview([FromBody] JobInterviewDto interview)
        {
            var result = await _userService.SetUpInterview(interview);
            return result;
        }

        [HttpGet("GetBlobFile")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task<ActionResult> GetBlobFile([FromQuery] string filePath)
        {
            var res = await _userService.Download(filePath);
            return File(res.Content, res.ContentType);
        }

    }
}
