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

namespace Ladaer.BackEnd.Controllers.Cms
{
    /// <summary>
    /// Controller d? x? lư các ho?t d?ng liên quan d?n bài vi?t.
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class PostController : ControllerBase
    {
        private readonly IPostService _postService;

        /// <summary>
        /// Kh?i t?o m?t instance m?i c?a l?p <see cref="PostController"/>.
        /// </summary>
        /// <param name="postService">D?ch v? x? lư bài vi?t.</param>
        public PostController(IPostService postService)
        {
            _postService = postService;
        }

        /// <summary>
        /// T́m ki?m các bài vi?t 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 = "Post.Search")]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var categories = await _postService.Search(param);
            return Ok(categories);
        }

        /// <summary>
        /// T?o ho?c c?p nh?t bài vi?t.
        /// </summary>
        /// <param name="postModel">Model ch?a thông tin bài vi?t.</param>
        /// <returns>K?t qu? c?a ho?t d?ng t?o ho?c c?p nh?t.</returns>
        [HttpPost("CreateOrUpdate")]
        [Authorize(Roles = "Post.CreateOrUpdate")]
        public async Task<IActionResult> CreateOrUpdate([FromBody] PostModel postModel)
        {
            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 d?nh du?c ngu?i dùng");
            }

            var result = await _postService.CreateOrUpdate(postModel);
            return Ok(result);
        }

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

        /// <summary>
        /// Xóa m?t bài vi?t d?a trên ID.
        /// </summary>
        /// <param name="id">ID c?a bài vi?t.</param>
        /// <returns>K?t qu? c?a ho?t d?ng xóa.</returns>
        [HttpDelete("Delete")]
        [Authorize(Roles = "Post.Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            var result = await _postService.Delete(id);
            return Ok(result);
        }

        /// <summary>
        /// Đang bài vi?t.
        /// </summary>
        /// <param name="model">Model ch?a thông tin tr?ng thái bài vi?t.</param>
        /// <returns>K?t qu? c?a ho?t d?ng dang bài vi?t.</returns>
        [HttpPut("Publish")]
        [Authorize(Roles = "Post.Publish")]
        public async Task<IActionResult> Publish(PostStatusModel model)
        {
            var currentUser = User.GetCurrentUser();
            model.Status = PostStatus.Published;
            model.UpdatedBy = currentUser.UserId;
            model.UpdatedByUserName = currentUser.UserName;
            var result = await _postService.UpdatePostStatus(model);
            return Ok(result);
        }

        /// <summary>
        /// T? ch?i bài vi?t.
        /// </summary>
        /// <param name="model">Model ch?a thông tin tr?ng thái bài vi?t.</param>
        /// <returns>K?t qu? c?a ho?t d?ng t? ch?i bài vi?t.</returns>
        [HttpPut("Reject")]
        [Authorize(Roles = "Post.Reject")]
        public async Task<IActionResult> Reject(PostStatusModel model)
        {
            var currentUser = User.GetCurrentUser();
            model.Status = PostStatus.Rejected;
            model.UpdatedBy = currentUser.UserId;
            model.UpdatedByUserName = currentUser.UserName;
            var result = await _postService.UpdatePostStatus(model);
            return Ok(result);
        }

        /// <summary>
        /// G?i bài vi?t d? dang.
        /// </summary>
        /// <param name="model">Model ch?a thông tin tr?ng thái bài vi?t.</param>
        /// <returns>K?t qu? c?a ho?t d?ng g?i bài vi?t d? dang.</returns>
        [HttpPut("SendToPublish")]
        [Authorize(Roles = "Post.SendToPublish")]
        public async Task<IActionResult> SendToPublish(PostStatusModel model)
        {
            var currentUser = User.GetCurrentUser();
            model.Status = PostStatus.Rejected;
            model.UpdatedBy = currentUser.UserId;
            model.UpdatedByUserName = currentUser.UserName;
            var result = await _postService.UpdatePostStatus(model);
            return Ok(result);
        }
    }
}
