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 ProductController : ControllerBase
    {
        private readonly IProductService _productService;

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

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


        /// <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 = "Product.GetById")]
        public async Task<IActionResult> GetById(int id)
        {
            var result = await _productService.GetById(id);
            return Ok(result);
        }


        /// <summary>
        /// T?o ho?c c?p nh?t d?ch v?.
        /// </summary>
        /// <param name="ProductModel">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 = "Product.CreateOrUpdate")]
        public async Task<IActionResult> CreateOrUpdate([FromBody] ProductModel ProductModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var currentUser = User.GetCurrentUser();
            ProductModel.UpdatedBy = currentUser.UserId;
            ProductModel.UpdatedByUserName = currentUser.UserName;
            if (ProductModel.UpdatedBy <= 0 || string.IsNullOrEmpty(ProductModel.UpdatedByUserName))
            {
                return BadRequest("Không xác d?nh du?c ngu?i dùng");
            }

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