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.Mvc;

namespace Ladaer.BackEnd.Controllers.Cms
{    
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class TopicController : ControllerBase
    {
        private readonly ITopicService _topicService;

        public TopicController(ITopicService topicService)
        {
            _topicService = topicService;
        }

        /// <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 = "Topic.Search")]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var topics = await _topicService.Search(param);
            return Ok(topics);
        }


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


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

            var result = await _topicService.CreateOrUpdate(TopicModel);
            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 = "Topic.Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            var result = await _topicService.DeleteAsync(id);
            return Ok(result);
        }

    }
}
