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

namespace Ladaer.BackEnd.Controllers.Systems
{
    /// <summary>
    /// Controller để quản lý các phiên bản đường dẫn (CallPathVersion).
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CallPathVersionController : ControllerBase
    {
        private readonly ICallPathVersionService _callPathVersionService;

        public CallPathVersionController(ICallPathVersionService callPathVersionService)
        {
            _callPathVersionService = callPathVersionService;
        }

        /// <summary>
        /// Lấy tất cả phiên bản (dùng cho dropdown).
        /// </summary>
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            var result = await _callPathVersionService.GetAllAsync();
            return Ok(result);
        }

        #region Search
        /// <summary>
        /// Tìm kiếm phiên bản theo các tham số.
        /// </summary>
        [HttpPost("Search")]
        [Authorize(Roles = "Founder")]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var result = await _callPathVersionService.SearchAsync(param);
            return Ok(result);
        }
        #endregion

        #region CRUD
        /// <summary>
        /// Tạo hoặc cập nhật phiên bản.
        /// </summary>
        [HttpPost("CreateOrUpdate")]
        [Authorize(Roles = "Founder")]
        public async Task<IActionResult> CreateOrUpdate([FromBody] CallPathVersionModel model)
        {
            var result = new BaseResult();
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            var currentUser = User.GetCurrentUser();
            model.UpdatedBy = currentUser.UserId;
            model.UpdatedByUserName = currentUser.UserName;

            if (model.UpdatedBy <= 0 || string.IsNullOrEmpty(model.UpdatedByUserName))
            {
                result.Result = Result.Failed;
                result.Message = $"Không xác định được người dùng {currentUser.UserId} - {currentUser.UserName} ";
                return Ok(result);
            }
            result = await _callPathVersionService.CreateOrUpdateAsync(model);
            return Ok(result);
        }

        /// <summary>
        /// Lấy thông tin chi tiết phiên bản theo ID.
        /// </summary>
        [HttpGet("GetById")]
        [Authorize(Roles = "Founder")]
        public async Task<IActionResult> GetById(int id)
        {
            var result = await _callPathVersionService.GetByIdAsync(id);
            return Ok(result);
        }

        /// <summary>
        /// Xóa phiên bản theo ID.
        /// </summary>
        [HttpDelete("Delete")]
        [Authorize(Roles = "Founder")]
        public async Task<IActionResult> Delete(int id)
        {
            var result = await _callPathVersionService.DeleteAsync(id);
            return Ok(result);
        }
        #endregion
    }
}
