using Lada.Framework.Data.Domains.Hr;
using Lada.Framework.DTO.SmartTable;
using Lada.Framework.Infrastructures.Helpers;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Hr;
using Ladaer.BackEnd.Infrastructures.Services.Hr;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Ladaer.BackEnd.Controllers.Hr
{
    /// <summary>
    /// Controller d? x? lư các ho?t d?ng liên quan d?n công vi?c.
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class JobController : ControllerBase
    {
        private readonly IJobPostService _jobPostService;

        /// <summary>
        /// Kh?i t?o m?t instance m?i c?a l?p <see cref="JobController"/>.
        /// </summary>
        /// <param name="jobPostService">D?ch v? x? lư công vi?c.</param>
        public JobController(IJobPostService jobPostService)
        {
            _jobPostService = jobPostService;
        }

        /// <summary>
        /// T́m ki?m các công vi?c d?a trên các tham s? du?c cung c?p.
        /// </summary>
        /// <param name="param">Các tham s? t́m ki?m.</param>
        /// <returns>K?t qu? t́m ki?m.</returns>
        [HttpPost("Search")]
        [Authorize(Roles = "Job.Search")]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var jobs = await _jobPostService.Search(param);
            return Ok(jobs);
        }

        /// <summary>
        /// T?o ho?c c?p nh?t thông tin công vi?c.
        /// </summary>
        /// <param name="jobModel">Model ch?a thông tin công vi?c.</param>
        /// <returns>K?t qu? c?a ho?t d?ng t?o ho?c c?p nh?t.</returns>
        [HttpPost("CreateOrUpdate")]
        [Authorize(Roles = "Job.CreateOrUpdate")]
        public async Task<IActionResult> CreateOrUpdate([FromBody] JobPostModel jobModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var currentUser = User.GetCurrentUser();
            jobModel.UpdatedBy = currentUser.UserId;
            jobModel.UpdatedByUserName = currentUser.UserName;
            if (jobModel.UpdatedBy <= 0 || string.IsNullOrEmpty(jobModel.UpdatedByUserName))
            {
                return BadRequest("Không xác d?nh du?c ngu?i dùng");
            }

            var result = await _jobPostService.CreateOrUpdate(jobModel);
            return Ok(result);
        }

        /// <summary>
        /// L?y thông tin chi ti?t c?a m?t công vi?c d?a trên ID.
        /// </summary>
        /// <param name="id">ID c?a công vi?c.</param>
        /// <returns>Thông tin chi ti?t c?a công vi?c.</returns>
        [HttpGet("GetById")]
        [Authorize(Roles = "Job.GetById")]
        public async Task<IActionResult> GetById(int id)
        {
            var result = await _jobPostService.GetById(id);
            return Ok(result);
        }

        /// <summary>
        /// Xóa m?t công vi?c d?a trên ID.
        /// </summary>
        /// <param name="id">ID c?a công vi?c.</param>
        /// <returns>K?t qu? c?a ho?t d?ng xóa.</returns>
        [HttpDelete("Delete")]
        [Authorize(Roles = "Job.Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            var currentUser = User.GetCurrentUser();
            if (currentUser == null)
            {
                return BadRequest("Không xác d?nh du?c ngu?i dùng");
            }
            var result = await _jobPostService.Delete(id, currentUser);
            return Ok(result);
        }

        #region Update Statuses
        [HttpPut("Approve")]
        [Authorize(Roles = "Founder")]
        public async Task<IActionResult> Approve([FromBody] JobStatusModel model)
        {
            var currentUser = User.GetCurrentUser();
            model.UpdatedBy = currentUser.UserId;
            model.UpdatedByUserName = currentUser.UserName;
            var result = await _jobPostService.UpdateStatus(model, JobStatus.Opening);
            return Ok(result);
        }

        [HttpPut("Close")]
        [Authorize(Roles = "Founder")]
        public async Task<IActionResult> Close([FromBody] JobStatusModel model)
        {
            var currentUser = User.GetCurrentUser();
            model.UpdatedBy = currentUser.UserId;
            model.UpdatedByUserName = currentUser.UserName;
            var result = await _jobPostService.UpdateStatus(model, JobStatus.Closed);
            return Ok(result);
        }
        #endregion
    }
}
