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

namespace Ladaer.BackEnd.Controllers.Ops
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class RouteController : ControllerBase
    {
        private readonly IRouteService _routeService;
        public RouteController(IRouteService routeService)
        {
            _routeService = routeService;
        }

        #region SEARCH
        /// <summary>
        /// T�m ki?m c�c tuy?n d?a tr�n tham s? SmartTableParam
        /// </summary>
        /// <param name="param">Tham s? t�m ki?m</param>
        /// <returns>Danh s�ch c�c tuy?n ph� h?p</returns>
        [HttpPost("Search")]
        [Authorize(Roles = "Route.Search")]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var routes = await _routeService.Search(param);
            return Ok(routes);
        }
        #endregion

        #region CRUD
        /// <summary>
        /// L?y th�ng tin tuy?n theo ID
        /// </summary>
        /// <param name="id">ID c?a tuy?n</param>
        /// <returns>Th�ng tin c?a tuy?n</returns>
        [HttpGet("GetById")]
        [Authorize(Roles = "Route.GetById")]
        public async Task<IActionResult> GetById(int id)
        {
            var routeModel = await _routeService.GetById(id);
            return Ok(routeModel);
        }

        /// <summary>
        /// X�a tuy?n theo ID
        /// </summary>
        /// <param name="id">ID c?a tuy?n</param>
        /// <returns>K?t qu? x�a</returns>
        [HttpDelete("Delete")]
        [Authorize(Roles = "Route.Delete")]
        public async Task<IActionResult> Delete(int id)
        {
            var deleteResult = await _routeService.Delete(id);
            return Ok(deleteResult);
        }

        /// <summary>
        /// T?o ho?c c?p nh?t th�ng tin tuy?n
        /// </summary>
        /// <param name="routeModel">Th�ng tin c?a tuy?n</param>
        /// <returns>K?t qu? t?o ho?c c?p nh?t</returns>
        [HttpPost("CreateOrUpdate")]
        [Authorize(Roles = "Route.CreateOrUpdate")]
        public async Task<IActionResult> CreateOrUpdate([FromBody] RouteModel routeModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            routeModel.UpdatedBy = User.GetUserId();
            routeModel.UpdatedByUserName = User.GetUsername();
            if (routeModel.UpdatedBy <= 0 || string.IsNullOrEmpty(routeModel.UpdatedByUserName))
            {
                return BadRequest("Kh�ng x�c d?nh du?c ngu?i d�ng");
            }

            var result = await _routeService.CreateOrUpdate(routeModel);
            return Ok(result);
        }
        #endregion
    }
}
