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

namespace User.Insight.Tracker.Controllers
{
    [ApiController]
    [Route("")]
    public class UserInsightTrackerController : ApiControllerBase
    {
        private readonly ILogger<UserInsightTrackerController> _logger;
        private readonly IUserInsightTrackerService _userTrackerService;
        
        public UserInsightTrackerController(ILogger<UserInsightTrackerController> logger, IUserInsightTrackerService userTrackerService, IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
        {
            _logger = logger;
            _userTrackerService = userTrackerService;          
        }


        [HttpPost]
        [Route("GetApplicationActivityReport")]
        [CustomExceptionFilter]
        public ActionResult<Payload<ApplicationActivityReportResponseResult>> GetApplicationActivityReport(ApplicationActivityReportRequest searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetApplicationActivityReport()");
            Payload<List<ApplicationActivityReportResponseResult>> response = new()
            {
                Data = _userTrackerService.GetApplicationActivityReport(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);

        }

        [HttpGet]
        [Route("GetAllActivity/{type}")]
        [CustomExceptionFilter]
        public ActionResult<Payload<ActivityDetailsResponse>> GetAllActivity(int type)
        {
            _logger.LogInformation("UserInsightTrackerController.GetAllActivity()");
            Payload<List<ActivityDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetAllActivity(type, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetAllUser")]
        [CustomExceptionFilter]
        public ActionResult<Payload<AllUserDetailsResponse>> GetAllUser(SearchRequestResponse<UserDetailedRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetAllUser()");
            Payload<PageResponseRequest<AllUserDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetAllUser(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetApplicationTabDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<ApplicationTabResponse>> GetApplicationTabDetails(SearchRequest<ApplicationTabRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetApplicationTabDetails()");
            Payload<PageResponse<ApplicationTabResponse>> response = new()
            {
                Data = _userTrackerService.GetApplicationTabDetails(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetBrowsingDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<BrowsingHistoryResponse>> GetBrowsingDetails(SearchRequest<BrowsingHistoryRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetBrowsingDetails()");
            Payload<PageResponse<BrowsingHistoryResponse>> response = new()
            {
                Data = _userTrackerService.GetBrowsingDetails(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [HttpPost]        
        [Route("GetCallDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<CallDetailsResponse>> GetCallDetails(SearchRequest<CallDetailFilterRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetCallDetails()");
            Payload<PageResponse<CallDetailsResponse>> response = new()
            {                
                Data = _userTrackerService.GetCallDetails(searchRequest,CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [HttpPost]
        [Route("GetCallsActivityReport")]
        [CustomExceptionFilter]
        public ActionResult<Payload<CallsActivityReportResponseResult>> GetCallsActivityReport(CallsActivityReportRequest searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetCallsActivityReport()");
            Payload<List<CallsActivityReportResponseResult>> response = new()
            {
                Data = _userTrackerService.GetCallsActivityReport(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [HttpPost]
        [Route("GetDashboardData")]
        [CustomExceptionFilter]
        public ActionResult<Payload<DashboardDetailsResponse>> GetDashboardData(SearchRequest<DashboardDetailsRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetDashboardData()");
            Payload<PageResponse<DashboardDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetDashboardData(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [HttpPost]
        [Route("GetEmployeeWorkingTime")]
        [CustomExceptionFilter]
        public ActionResult<Payload<EmployeeWorkingTimeResponse>> GetEmployeeWorkingTime(SearchRequest<EmployeeWorkingTimeRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetEmployeeWorkingTime()");
            Payload<PageResponse<EmployeeWorkingTimeResponse>> response = new()
            {
                Data = _userTrackerService.GetEmployeeWorkingTime(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [HttpPost]
        [Route("GetGraphicData")]
        [CustomExceptionFilter]
        public ActionResult<Payload<GraphDetailsResponse>> GetGraphicData(SearchRequest<GraphDetailsRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetGraphicData()");
            Payload<PageResponse<GraphDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetGraphicData(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetLogInActivityReport")]
        [CustomExceptionFilter]
        public ActionResult<Payload<LogInActivityReportResponse>> GetLogInActivityReport(LogInActivityReportRequest searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetLogInActivityReport()");
            Payload<List<LogInActivityReportResponseResult>> response = new()
            {
                Data = _userTrackerService.GetLogInActivityReport(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);

        }

        [HttpPost]
        [Route("GetSystemSleepDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<SystemSleepDetailResponse>> GetSystemSleepDetails(SearchRequest<SystemSleepRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.SystemSleepDetailResponse()");
            Payload<PageResponse<SystemSleepDetailResponse>> response = new()
            {
                Data = _userTrackerService.GetSystemSleepDetails(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [HttpPost]
        [Route("GetSystemLoginDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<SystemLoginDetailsResponse>> GetSystemLoginDetails(SearchRequest<SystemLoginDetailsRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetSystemLoginDetails()");
            Payload<PageResponse<SystemLoginDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetSystemLoginDetails(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [HttpPost]
        [Route("GetRunningApplications")]
        [CustomExceptionFilter]
        public async Task<ActionResult<Payload<RunningApplicationResponse>>> GetRunningApplications(SearchRequest<RunningApplicationRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetRunningApplications()");
            Payload<PageResponse<RunningApplicationResponse>> response = new()
            {
                Data = await _userTrackerService.GetRunningApplications(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }  

        [HttpPost]
        [Route("GetUserProfileData")]
        [CustomExceptionFilter]
        public ActionResult<Payload<UserProfileResponse>> GetUserProfileData(SearchRequest<UserProfileRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetUserProfileData()");
            Payload<PageResponse<UserProfileResponse>> response = new()
            {
                Data = _userTrackerService.GetUserProfileData(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetUserSummaryDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<SummaryDetailsResponse>> GetUserSummaryDetails(SearchRequestResponse<SummaryDetailsRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetAllUser()");
            Payload<PageResponseRequest<SummaryDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetUserSummaryDetails(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [HttpPost]
        [Route("GetUserActivityDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<UserActivityDetailsResponse>> GetUserActivityDetails(SearchRequest<UserActivityDetailsRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetUserActivityDetails()");
            Payload<PageResponse<UserActivityDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetUserActivityDetails(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }          

        [HttpPost]
        [Route("GetSessionsActivityReport")]
        [CustomExceptionFilter]
        public ActionResult<Payload<SessionActivityReportResponse>> GetSessionsActivityReport(SessionActivityReportRequest searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetSessionsActivityReport()");
            Payload<List<SessionActivityReportResponseResult>> response = new()
            {
                Data = _userTrackerService.GetSessionsActivityReport(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetSummaryTopBottomDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<TopBottomDetailsResponseResult>> GetSummaryTopBottomDetails(SummaryTopBottomDetails searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetSummaryTopBottomDetails()");
            Payload<PageResponse<TopBottomDetailsResponseResult>> response = new()
            {
                Data = _userTrackerService.GetSummaryTopBottomDetails(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpGet]
        [Route("GetReportActivity")]
        [CustomExceptionFilter]
        public ActionResult<ReportActivityResponseData> GetReportActivity()
        {
            _logger.LogInformation("UserInsightTrackerController.GetReportActivity()");

            // Fetch the data from the service
            var responsePayload = _userTrackerService.GetReportActivity(CurrentUserJWT);

            // Wrap the response in a Payload object
            var response = new Payload<ReportActivityResponseData>
            {
                Data = responsePayload,
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };

            // Return the response with a status code of 200 OK
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetRunningApplicationsDetailed")]
        [CustomExceptionFilter]
        public ActionResult<Payload<ApplicationTabDetailsResponse>> GetRunningApplicationsDetailed(SearchRequest<ApplicationTabDetailsRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetRunningApplicationsDetailed()");
            Payload<PageResponse<ApplicationTabDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetRunningApplicationsDetailed(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetUserExists")]
        [CustomExceptionFilter]
        public async Task<ActionResult<Payload<AllUserDetailsUser>>> GetUserExists()
        {
            _logger.LogInformation("UserInsightTrackerController.GetUserExists()");
            var data =await _userTrackerService.GetUserExists(CurrentUserJWT);

            var response = new Payload<PageResponseRequest<AllUserDetailsUser>>
            {
                Data = data,
                Message = data.Records == null ? Constant.ErrorMsg : Constant.MsgSuccess,
                IsSuccess = data.Records != null
            };   
            
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("AddUpdateUser")]
        [CustomExceptionFilter]
        public ActionResult<UserActiveResponse> AddUpdateUser(AddUpdateUserRequest searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.AddUpdateUser()");
            var data = _userTrackerService.AddUpdateUser(searchRequest, CurrentUserJWT);
            var response = new Payload<UserActiveResponse>
            {
                Message = data.Message != "Success" ? data.Message : Constant.MsgSuccess,
                IsSuccess = data.Message == "Success"
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("DeleteUser")]
        [CustomExceptionFilter]
        public ActionResult<DeleteUserResponse> DeleteUser(DeleteUserRequest searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.DeleteUser()");
            Payload<DeleteUserResponse> response = new()
            {
                Data = _userTrackerService.DeleteUser(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);

        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("UserActive")]
        [CustomExceptionFilter]
        public ActionResult<UserActiveResponse> UserActive(UserActiveRequest searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.UserActive()");
            Payload<UserActiveResponse> response = new()
            {
                Data = _userTrackerService.UserActive(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetLocationDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<LocationDetailsResponse>> GetLocationDetails(SearchRequest<LocationDetailsRequest> searchRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetLocationDetails()");
            Payload<PageResponse<LocationDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetLocationDetails(searchRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetPermissionDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<PermissionResponseResult>> GetPermissionDetails(PermissionDetailRequest permissionDetailRequest)
        {
            _logger.LogInformation("UserInsightTrackerController.GetPermissionDetails()");
            Payload<List<PermissionResponseResult>> response = new()
            {
                Data = _userTrackerService.GetPermissionDetails(permissionDetailRequest, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);

        }


        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetManagerRoles")]
        [CustomExceptionFilter]
        public ActionResult<Payload<ManagerRolesResponse>> GetManagerRoles(ManagerRolesRequest managerRoles)
        {
            _logger.LogInformation("UserInsightTrackerController.GetManagerRoles()");
            Payload<PageResponse<ManagerRolesResponse>> response = new()
            {
                Data = _userTrackerService.GetManagerRoles(managerRoles, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);

        }


        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("GetRoleDetails")]
        [CustomExceptionFilter]
        public ActionResult<Payload<RoleDetailsResponse>> GetRoleDetails(RoleDetailsRequest roleDetails)
        {
            _logger.LogInformation("UserInsightTrackerController.GetRoleDetails()");
            Payload<PageResponse<RoleDetailsResponse>> response = new()
            {
                Data = _userTrackerService.GetRoleDetails(roleDetails, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);

        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("CreateOrModifyRoles")]
        [CustomExceptionFilter]
        public ActionResult<Payload<CreateRolesResponse>> CreateOrModifyRoles(CreateRolesRequest createRoles)
        {
            _logger.LogInformation("UserInsightTrackerController.CreateOrModifyRoles()");
            Payload<PageResponse<CreateRolesResponse>> response = new()
            {
                Data = _userTrackerService.CreateOrModifyRoles(createRoles, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);

        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("UpdateRolePermission")]
        [CustomExceptionFilter]
        public ActionResult<Payload<UpdateRoleResponse>> UpdateRolePermission(List<UpdateRoleRequest> roleRequests)
        {
            _logger.LogInformation("UserInsightTrackerController.UpdateRolePermission()");
            Payload<PageResponse<UpdateRoleResponse>> response = new()
            {
                Data = _userTrackerService.UpdateRolePermission(roleRequests, CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };

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


        [ExcludeFromCodeCoverage]
        [HttpGet]
        [Route("GetAllApplication")]
        [CustomExceptionFilter]
        public ActionResult<Payload<ApplicationPropertyResponse>> GetAllApplication()
        {
            _logger.LogInformation("UserInsightTrackerController.GetAllApplication()");
            Payload<PageResponse<ApplicationPropertyResponse >> response = new()
            {
                Data = _userTrackerService.GetAllApplication(CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

        [ExcludeFromCodeCoverage]
        [HttpGet]
        [Route("GetAllMenu")]
        [CustomExceptionFilter]
        public ActionResult<Payload<MenuListResponse>> GetAllMenu()
        {
            _logger.LogInformation("UserInsightTrackerController.GetAllMenu()");
            Payload<PageResponse<MenuListResponse>> response = new()
            {
                Data = _userTrackerService.GetAllMenu(CurrentUserJWT),
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };
            return StatusCode((int)HttpStatusCode.OK, response);
        }

    }
}