using Microsoft.AspNetCore.Mvc;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using User.Insight.Tracker.Common;
using User.Insight.Tracker.DataContracts.Response;
using User.Insight.Tracker.DataContracts;
using User.Insight.Tracker.Services;
using User.Insight.Tracker.DataContracts.Requests;
using DataBase.Entities.Models;

namespace User.Insight.Tracker.Controllers
{
    [ExcludeFromCodeCoverage]
    [ApiController]
    [Route("")]
    public class CommonController : ApiControllerBase
    {
        private readonly ILogger<CommonController> _logger;
        private readonly ICommonService _userTrackerService;

        public CommonController(ILogger<CommonController> logger, ICommonService commonService, IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
        {
            _logger = logger;
            _userTrackerService = commonService;
        }

        [ExcludeFromCodeCoverage]
        [HttpPost("[action]")]
        [CustomExceptionFilter]
        public ActionResult<Payload<PageResponse<AuditResponse>>> GetAudit(SearchRequest<AuditRequest> searchRequest)
        {
            _logger.LogInformation("CommonController.GetAudit()");

            var response = new Payload<PageResponse<AuditResponse>>
            {
                Data = _userTrackerService.GetAudit(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };

            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost("[action]")]
        [CustomExceptionFilter]
        public ActionResult<Payload<PageResponse<DeleteRoleResponse>>> DeleteRoles(SearchRequest<DeleteRoleRequest> searchRequest)
        {
            _logger.LogInformation("CommonController.DeleteRoles()");

            var response = new Payload<PageResponse<DeleteRoleResponse>>
            {
                Data = _userTrackerService.DeleteRoles(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };

            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpGet("[action]")]
        [CustomExceptionFilter]
        public ActionResult<Payload<SmartSearchResponse>> GetSmartSearch(string EmpCode)
        {
            _logger.LogInformation("CommonController.GetSmartSearch()");
            Payload<List<SmartSearchResponse>> response = new()
            {
                Data = _userTrackerService.GetSmartSearch(CurrentUserJWT , EmpCode),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost("[action]")]
        [CustomExceptionFilter]
        public ActionResult<Payload<PageResponse<GlobalConfigurationResponse>>> GetGlobalConfiguration(SearchRequest<GlobalConfigurationRequest> searchRequest)
        {
            _logger.LogInformation("CommonController.GetGlobalConfiguration()");

            var response = new Payload<PageResponse<GlobalConfigurationResponse>>
            {
                Data = _userTrackerService.GetGlobalConfiguration(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };

            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost("[action]")]
        [CustomExceptionFilter]
        public ActionResult<Payload<PageResponse<GlobalConfigurationUpdatePropertiesStatusResponse>>> GlobalPropertiesStatus(SearchRequest<GlobalConfigurationUpdatePropertiesStatus> searchRequest)
        {
            _logger.LogInformation("CommonController.GlobalPropertiesStatus()");

            var response = new Payload<PageResponse<GlobalConfigurationUpdatePropertiesStatusResponse>>
            {
                Data = _userTrackerService.GlobalPropertiesStatus(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };

            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost("[action]")]
        [CustomExceptionFilter]
        public ActionResult<Payload<PageResponse<GlobalConfigurationUpdatePropertiesValueResponse>>> GlobalPropertiesValue(SearchRequest<GlobalConfigurationUpdatePropertiesRequest> searchRequest)
        {
            _logger.LogInformation("CommonController.GlobalPropertiesValue()");

            var response = new Payload<PageResponse<GlobalConfigurationUpdatePropertiesValueResponse>>
            {
                Data = _userTrackerService.GlobalPropertiesValue(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };

            return StatusCode((int)HttpStatusCode.OK, response);
        }
        [ExcludeFromCodeCoverage]
        [HttpPost("sync")]
        [CustomExceptionFilter]
        public async Task<IActionResult> Sync()
        {
            await _userTrackerService.SyncStafflineUsersAsync();
            return Ok(new { Message = "Staffline sync completed successfully." });
        }
    }
}
