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

namespace Ladaer.BackEnd.Controllers.Crm
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class LeadController : ControllerBase
    {
        private readonly ILeadService _leadService;

        public LeadController(ILeadService leadService)
        {
            _leadService = leadService;
        }


        /// <summary>
        /// T́m ki?m lead
        /// </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 = "Lead.Search")]
        public async Task<IActionResult> Search([FromBody] SmartTableParam param)
        {
            var agencies = await _leadService.Search(param);
            return Ok(agencies);
        }

        /// <summary>
        /// T?o ho?c c?p nh?t lead
        /// </summary>
        /// <param name="model">Model ch?a thông tin lead c?n t?o/c?p nh?t.</param>
        /// <returns>K?t qu? c?a ho?t d?ng t?o/c?p nh?t lead.</returns>
        [HttpPost("CreateOrUpdate")]
        [Authorize(Roles = "Lead.CreateOrUpdate")]
        public async Task<IActionResult> CreateOrUpdate([FromBody] LeadModel model)
        {
            var result = await _leadService.CreateOrUpdate(model);
            return Ok(result);
        }

        /// <summary>
        /// Gán ngu?i qu?n lư cho m?t lead.
        /// </summary>
        /// <param name="model">Model ch?a thông tin ngu?i qu?n lư c?n gán.</param>
        /// <returns>K?t qu? c?a ho?t d?ng gán ngu?i qu?n lư.</returns>
        [HttpPut("AssignManager")]
        [Authorize(Roles = "Lead.AssignManager")]
        public async Task<IActionResult> AssignManager([FromBody] LeadStatusModel model)
        {
            var result = new BaseResult();
            var currentUser = User.GetCurrentUser();
            if (currentUser.UserId <= 0 || string.IsNullOrEmpty(currentUser.UserName))
            {
                result.Result = Result.Failed;
                result.Message = "Không xác d?nh du?c ngu?i dùng";
            }
            else
            {
                model.PicId = currentUser.UserId;
                model.PicUserName = currentUser.UserName;
                model.UpdatedBy = currentUser.UserId;
                model.UpdatedByUserName = currentUser.UserName;
                model.LeadStatus = LeadStatus.Assigned;
                result = await _leadService.UpdateLeadStatus(model);
            }

            return Ok(result);
        }


        /// <summary>
        /// L?y thông tin dánh giá c?a m?t lead d?a trên ID.
        /// </summary>
        /// <param name="id">ID c?a lead.</param>
        /// <returns>Thông tin dánh giá c?a lead.</returns>
        [HttpGet("GetReviewModel")]
        [Authorize(Roles = "Lead.GetReviewModel")]
        public async Task<IActionResult> GetReviewModel(int id)
        {
            var result = await _leadService.GetReviewModelById(id);
            return Ok(result);
        }


        /// <summary>
        /// C?p nh?t thông tin dánh giá c?a m?t lead.
        /// </summary>
        /// <param name="model">Model ch?a thông tin dánh giá c?a lead.</param>
        /// <returns>K?t qu? c?a ho?t d?ng c?p nh?t dánh giá.</returns>
        [HttpPut("UpdateReview")]
        [Authorize(Roles = "Lead.UpdateReview")]
        public async Task<IActionResult> UpdateReview([FromBody] LeadStatusModel model)
        {
            var result = await _leadService.UpdateLeadStatus(model);
            return Ok(result);
        }
    }
}
