using Ladaer.BackEnd.Infrastructures.Services.Cms;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Cms;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

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

        public UrlRecordController(IUrlRecordService urlRecordService)
        {
            _urlRecordService = urlRecordService;
        }


        [HttpGet("GetById")]
        [Authorize(Roles = "UrlRecord.GetById")]
        public async Task<IActionResult> GetById(int id)
        {
            var result = await _urlRecordService.GetById(id);
            return Ok(result);
        }

        [HttpPost("search")]
        [Authorize(Roles = "UrlRecord.Search")]
        public async Task<IActionResult> Search([FromBody] Lada.Framework.DTO.SmartTable.SmartTableParam param)
        {
            var result = await _urlRecordService.Search(param);
            return Ok(result);

        }

        /// <summary>
        /// C?p nh?t thông tin co b?n c?a UrlRecord
        /// </summary>
        /// <param name="model">Model ch?a thông tin c?p nh?t</param>
        /// <returns>K?t qu? c?p nh?t</returns>
        [HttpPut("UpdateInfo")]
        [Authorize(Roles = "UrlRecord.UpdateInfo")]
        public async Task<IActionResult> UpdateInfo([FromBody] UrlRecordUpdateModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var result = await _urlRecordService.UpdateUrlRecordInfo(model);
            return Ok(result);
        }
    }
}
