using DataBase.Entities.Models;
using System.Diagnostics.CodeAnalysis;
using User.Insight.Tracker.Common;
using User.Insight.Tracker.DataContracts;
using User.Insight.Tracker.DataContracts.Requests;
using User.Insight.Tracker.DataContracts.Response;
using User.Insight.Tracker.DataProviders;

namespace User.Insight.Tracker.Services
{
   
    public class UserInsightTrackerService : IUserInsightTrackerService
    {
        private readonly ILogger<UserInsightTrackerService> _logger;
        private readonly IUserInsightTrackerProvider _userTrackerProvider;
        private readonly IUtils _utils;
        public UserInsightTrackerService(ILogger<UserInsightTrackerService> logger, IUserInsightTrackerProvider userTrackerProvider, IUtils utils)
        {
            _logger = logger;
            _userTrackerProvider = userTrackerProvider;
            _utils = utils;
        }
        [ExcludeFromCodeCoverage]
        public PageResponse<CallDetailsResponse> GetCallDetails(SearchRequest<CallDetailFilterRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetCallDetails called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            PageResponse<CallDetailsResponse> data = new PageResponse<CallDetailsResponse>();
            var response = _userTrackerProvider.GetEmployeeCallDetails(searchRequest , userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);
                var listResponse = response.Select(e => new CallDetailsResponse()
                {
                    Id = e.Id,
                    EmpCode = e.EmpCode,
                    CallWith = e.CallWith,
                    CallStartedOn = e.CallStartedOn,
                    CallEndedOn = e.CallEndedOn,
                    CreatedOn = e.CreatedOn,
                    UpdatedOn = e.UpdatedOn,
                    Total_Call_Duration=e.Total_Call_Duration,
                    EmployeeName=e.EmployeeName,
                }).ToList();
                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;
            }
            return data;
        }
        [ExcludeFromCodeCoverage]
        public PageResponse<SystemSleepDetailResponse> GetSystemSleepDetails(SearchRequest<SystemSleepRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetSystemSleepDetails called with {@SearchRequest}", searchRequest);
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<SystemSleepDetailResponse> data = new PageResponse<SystemSleepDetailResponse>();
            var response = _userTrackerProvider.GetSystemSleepDetails(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new SystemSleepDetailResponse()
                {
                    Id = e.Id,
                    EmpCode = e.EmpCode,
                    Description = e.Description,
                    TriggeredOn = e.TriggeredOn,
                    CreatedOn = e.CreatedOn,
                    UpdatedOn = e.UpdatedOn,
                    Total_Sleep_Time=e.Total_Sleep_Time,
                    EmployeeName = e.EmployeeName
                }).ToList();

                if (searchRequest.PageNumber > total_Pages && total_Pages > 0)
                    searchRequest.PageNumber = total_Pages;

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;

            }
            return data;
        }
        [ExcludeFromCodeCoverage]
        public PageResponse<EmployeeWorkingTimeResponse> GetEmployeeWorkingTime(SearchRequest<EmployeeWorkingTimeRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetEmployeeWorkingTime called with {@SearchRequest}", searchRequest);
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<EmployeeWorkingTimeResponse> data = new PageResponse<EmployeeWorkingTimeResponse>();
            var response = _userTrackerProvider.GetEmployeeWorkingTime(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new EmployeeWorkingTimeResponse()
                {
                    Id = e.Id,
                    EmpCode = e.EmpCode,
                    WorkLoggedOn = e.WorkLoggedOn,
                    TotalActive = e.TotalActive,
                    TotalIdle = e.TotalIdle,
                    TotalSleep = e.TotalSleep,
                    CreatedOn = e.CreatedOn,
                    UpdatedOn = e.UpdatedOn,
                    EmployeeName = e.EmployeeName,
                    Location_updated_on=e.Location_updated_on,
                    Location=e.Location
                }).ToList();

                if (searchRequest.PageNumber > total_Pages && total_Pages > 0)
                    searchRequest.PageNumber = total_Pages;

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;

            }
            return data;
        }
        [ExcludeFromCodeCoverage]
        public PageResponse<SystemLoginDetailsResponse> GetSystemLoginDetails(SearchRequest<SystemLoginDetailsRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetSystemLoginDetails called with {@SearchRequest}", searchRequest);
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<SystemLoginDetailsResponse> data = new PageResponse<SystemLoginDetailsResponse>();
            var response = _userTrackerProvider.GetSystemLoginDetails(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new SystemLoginDetailsResponse()
                {
                    Id = e.Id,
                    EmpCode = e.EmpCode,
                    LoginTime = e.LoginTime,
                    LogoutTime = e.LogoutTime,
                    SystemStatus = e.SystemStatus,
                    IpAddress = e.IpAddress,
                    ComputerName = e.ComputerName,
                    AppVersion = e.AppVersion,
                    CreatedOn = e.CreatedOn,
                    UpdatedOn = e.UpdatedOn,
                    Total_Login_Time= e.Total_Login_Time,
                    EmployeeName = e.EmployeeName
                }).ToList();

                if (searchRequest.PageNumber > total_Pages && total_Pages > 0)
                    searchRequest.PageNumber = total_Pages;

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;

            }
            return data;
        }
        [ExcludeFromCodeCoverage]

        public async Task<PageResponse<RunningApplicationResponse>> GetRunningApplications(SearchRequest<RunningApplicationRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetRunningApplications called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<RunningApplicationResponse> data = new PageResponse<RunningApplicationResponse>();
            var response = await _userTrackerProvider.GetRunningApplications(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new RunningApplicationResponse()
                {
                    Id = e.Id,
                    EmpCode = e.EmpCode,
                    ApplicationName = e.ApplicationName,
                    StartedOn = e.StartedOn,
                    CreatedOn = e.CreatedOn,
                    EndedOn = e.EndedOn,
                    UpdatedOn = e.UpdatedOn,
                    Description = e.Description,
                    Total_Browsing_time= e.Total_Browsing_time
                }).ToList();

                if (searchRequest.PageNumber > total_Pages && total_Pages > 0)
                    searchRequest.PageNumber = total_Pages;

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;

            }
            return data;
        }
        [ExcludeFromCodeCoverage]
        public PageResponse<GraphDetailsResponse> GetGraphicData(SearchRequest<GraphDetailsRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetGraphicData called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<GraphDetailsResponse> data = new PageResponse<GraphDetailsResponse>();
            var response = _userTrackerProvider.GetGraphicData(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                var listResponse = response.Select(e => new GraphDetailsResponse()
                {
                    ActiveLoginCount = e.ActiveLoginCount,
                    LoginCount = e.LoginCount,
                    EndTime = e.EndTime

                }).ToList();

                data.Records = listResponse;
                data.TotalRecords = 1;
                data.TotalPages = 1;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;
            }



            return data;
        }

        [ExcludeFromCodeCoverage]
        public PageResponse<DashboardDetailsResponse> GetDashboardData(SearchRequest<DashboardDetailsRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetDashboardData called with {@SearchRequest}", searchRequest);
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<DashboardDetailsResponse> data = new PageResponse<DashboardDetailsResponse>();
            var response = _userTrackerProvider.GetDashboardData(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                var listResponse = response.Select(e => new DashboardDetailsResponse()
                {
                    Device = e.Device,
                    Version = e.Version,
                    EmpCode = e.EmpCode,
                    LoginTime = e.LoginTime,
                    SleepCount = e.SleepCount,
                    IdleCount = e.IdleCount,
                    ActiveCount = e.ActiveCount,
                    TotalUsageCount = e.TotalUsageCount,
                    TotalActiveUsers = e.TotalActiveUsers,
                    TotalUsers = e.TotalUsers,
                    TotalActiveUsersToday = e.TotalActiveUsersToday

                }).ToList();

                data.Records = listResponse;
                data.TotalRecords = 1;
                data.TotalPages = 1;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;
            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public PageResponse<UserProfileResponse> GetUserProfileData(SearchRequest<UserProfileRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetUserProfileData called with {@SearchRequest}", searchRequest);
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<UserProfileResponse> data = new PageResponse<UserProfileResponse>();
            var response = _userTrackerProvider.GetUserProfileData(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new UserProfileResponse()
                {
                    EmpCode = e.EmpCode,
                    last_work_logged_on = e.last_work_logged_on,
                    TotalActive = e.TotalActive,
                    TotalIdle = e.TotalIdle,
                    TotalSleep = e.TotalSleep,
                    CreatedOn = e.CreatedOn,
                    EmployeeName = e.EmployeeName,
                    Location = e.Location,
                    Department = e.Department
                }).ToList();

                if (searchRequest.PageNumber > total_Pages && total_Pages > 0)
                    searchRequest.PageNumber = total_Pages;

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;

            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public List<ActivityDetailsResponse> GetAllActivity(int Type, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.GetAllActivity");           
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            List<ActivityDetailsResponse> data = new List<ActivityDetailsResponse>();
            var response = _userTrackerProvider.GetAllActivity(Type ,userDetails);
            var firstRecord = response.ToList();
            if (firstRecord != null)
            {
                data = response.Select(e => new ActivityDetailsResponse()
                {
                    Id = e.Id,
                    Name = e.Name,
                    Type = e.Type,
                    UniqueId = e.UniqueId

                }).ToList();

            }
            return data;
        }
        [ExcludeFromCodeCoverage]
        public PageResponse<UserActivityDetailsResponse> GetUserActivityDetails(SearchRequest<UserActivityDetailsRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetUserActivityDetails called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<UserActivityDetailsResponse> data = new PageResponse<UserActivityDetailsResponse>();
            var response = _userTrackerProvider.GetUserActivityDetails(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new UserActivityDetailsResponse()
                {
                    Id = e.Id,
                    EmpCode = e.EmpCode,
                    LoginTime = e.LoginTime,
                    LogoutTime = e.LogoutTime,
                    SystemStatus = e.SystemStatus,
                    IpAddress = e.IpAddress,
                    CreatedOn = e.CreatedOn,
                    UpdatedOn = e.UpdatedOn,
                    EmployeeName = e.EmployeeName


                }).ToList();

                if (searchRequest.PageNumber > total_Pages && total_Pages > 0)
                    searchRequest.PageNumber = total_Pages;

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;

            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public List<ApplicationActivityReportResponseResult> GetApplicationActivityReport(ApplicationActivityReportRequest searchRequest, JwtTokenDetail userDetails)
        {

            _logger.LogInformation("UserInsightTrackerService.GetApplicationActivityReport called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            List<ApplicationActivityReportResponse> resultsList = new List<ApplicationActivityReportResponse>();
            List<ApplicationActivityReportResponseResult> objApplicationActivityReportResultList = new List<ApplicationActivityReportResponseResult>();
            ApplicationActivityReportResponseResult objApplicationActivityReportResult = new ApplicationActivityReportResponseResult();

            var response = _userTrackerProvider.GetApplicationActivityReport(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();

            if (firstRecord != null)
            {
                foreach (var record in firstRecord.Results)
                {
                    ApplicationActivityReportResponse results = new ApplicationActivityReportResponse
                    {
                        ApplicationName = record.ApplicationName,
                        ApplicationCount = record.ApplicationCount,
                        StartedOn = record.StartedOn
                    };
                    resultsList.Add(results);
                }

                objApplicationActivityReportResult.TotalCount = firstRecord.TotalCount;
                objApplicationActivityReportResult.Results = resultsList;
                objApplicationActivityReportResultList.Add(objApplicationActivityReportResult);
            }

            return objApplicationActivityReportResultList;
        }

        [ExcludeFromCodeCoverage]
        public List<CallsActivityReportResponseResult> GetCallsActivityReport(CallsActivityReportRequest searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetCallsActivityReport called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            List<CallsActivityReportResponse> resultsList = new List<CallsActivityReportResponse>();
            List<CallsActivityReportResponseResult> objApplicationActivityReportResultList = new List<CallsActivityReportResponseResult>();
            CallsActivityReportResponseResult objApplicationActivityReportResult = new CallsActivityReportResponseResult();

            var response = _userTrackerProvider.GetCallsActivityReport(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();

            if (firstRecord != null)
            {
                foreach (var record in firstRecord.Results)
                {
                    CallsActivityReportResponse results = new CallsActivityReportResponse
                    {
                        Callwith = record.Callwith,
                        CallsCount = record.CallsCount,
                        StartedOn = record.StartedOn,
                        EmpCode = record.EmpCode
                    };
                    resultsList.Add(results);
                }

                objApplicationActivityReportResult.TotalCount = firstRecord.TotalCount;
                objApplicationActivityReportResult.Results = resultsList;
                objApplicationActivityReportResultList.Add(objApplicationActivityReportResult);
            }

            return objApplicationActivityReportResultList;
        }



        [ExcludeFromCodeCoverage]
        public List<SessionActivityReportResponseResult> GetSessionsActivityReport(SessionActivityReportRequest searchRequest, JwtTokenDetail userDetails)
        {            
            _logger.LogInformation("UserInsightTrackerService.GetSessionsActivityReport called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            List<SessionActivityReportResponse> resultsList = new List<SessionActivityReportResponse>();
            List<SessionActivityReportResponseResult> objApplicationActivityReportResultList = new List<SessionActivityReportResponseResult>();
            SessionActivityReportResponseResult objApplicationActivityReportResult = new SessionActivityReportResponseResult();

            var response = _userTrackerProvider.GetSessionsActivityReport(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();

            if (firstRecord != null)
            {
                foreach (var record in firstRecord.Results)
                {
                    SessionActivityReportResponse results = new SessionActivityReportResponse
                    {
                        TotalCount = record.TotalCount,
                        ActiveCount = record.ActiveCount,
                        LoginDate = record.LoginDate,
                        SleepCount = record.SleepCount,
                        IdleCount = record.IdleCount,

                    };
                    resultsList.Add(results);
                }

                objApplicationActivityReportResult.TotalCount = firstRecord.TotalCount;
                objApplicationActivityReportResult.Results = resultsList;
                objApplicationActivityReportResultList.Add(objApplicationActivityReportResult);
            }

            return objApplicationActivityReportResultList;
        }


        [ExcludeFromCodeCoverage]
        public List<LogInActivityReportResponseResult> GetLogInActivityReport(LogInActivityReportRequest searchRequest, JwtTokenDetail userDetails)
        {           
            _logger.LogInformation("UserInsightTrackerService.GetLogInActivityReport called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            List<LogInActivityReportResponse> resultsList = new List<LogInActivityReportResponse>();
            List<LogInActivityReportResponseResult> objApplicationActivityReportResultList = new List<LogInActivityReportResponseResult>();
            LogInActivityReportResponseResult objApplicationActivityReportResult = new LogInActivityReportResponseResult();

            var response = _userTrackerProvider.GetLogInActivityReport(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();

            if (firstRecord != null)
            {
                foreach (var record in firstRecord.Results)
                {
                    LogInActivityReportResponse results = new LogInActivityReportResponse
                    {
                        LogInDate = record.LogInDate,
                        LogInCount = record.LogInCount,
                        LogOutCount = record.LogOutCount,

                    };
                    resultsList.Add(results);
                }

                objApplicationActivityReportResult.Results = resultsList;
                objApplicationActivityReportResultList.Add(objApplicationActivityReportResult);
            }

            return objApplicationActivityReportResultList;
        }


        [ExcludeFromCodeCoverage]
        public ReportActivityResponseData GetReportActivity(JwtTokenDetail userDetails)
        {          
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.GetReportActivity");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            // List to store the results
            List<ReportActivityResponseResults> objApplicationActivityReportResultList = new List<ReportActivityResponseResults>();

            // Fetch report activity and details
            var response = _userTrackerProvider.GetReportActivity(userDetails);
            var details = _userTrackerProvider.GetAllActivity(Constant.Type ,userDetails);

            // Create the details response list
            List<DetailsResponse> detailsList = new List<DetailsResponse>();
            foreach (var detail in details)
            {
                DetailsResponse objDetails = new DetailsResponse
                {
                    Name = detail.Name,
                };

                detailsList.Add(objDetails);
            }

            // Process each record in the response
            foreach (var record in response)
            {
                List<ReportActivityResponse> listAR = new List<ReportActivityResponse>();

                foreach (var item in record.ActivityType)
                {
                    ReportActivityResponse objAR = new ReportActivityResponse
                    {
                        UniqueId = item.UniqueId,
                        Id = item.Id,
                        Name = item.Name,
                        Type = item.Type // Assuming this is part of the item object
                    };

                    listAR.Add(objAR);
                }

                ReportActivityResponseResults results = new ReportActivityResponseResults
                {
                    ParentName = record.ParentName,
                    ParentId = record.ParentId,
                    ActivityTypeData = listAR,
                };

                objApplicationActivityReportResultList.Add(results);
            }

            // Wrap the results in the payload
            ReportActivityResponseData responseData = new ReportActivityResponseData
            {
                ActivityType = objApplicationActivityReportResultList,
                DeviceType = detailsList,
            };
            return responseData;
        }

        [ExcludeFromCodeCoverage]
        public PageResponseRequest<AllUserDetailsResponse> GetAllUser(SearchRequestResponse<UserDetailedRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.GetAllUser");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            PageResponseRequest<AllUserDetailsResponse> data = new PageResponseRequest<AllUserDetailsResponse>();
            var response = _userTrackerProvider.GetAllUser(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.total_records);
                int total_Pages = Convert.ToInt32(firstRecord.total_pages);
                var listResponse = response.Select(record => new AllUserDetailsResponse()
                {
                    user_id = record.user_id,
                    user_first_name = record.user_first_name,
                    user_last_name = record.user_last_name,
                    user_email = record.user_email,
                    user_status = record.user_status,
                    user_role_id = record.user_role_id,
                    user_role_name = record.user_role_name,
                    is_active = record.is_active,
                    is_deleted = record.is_deleted,
                    created_on = record.created_on,
                    updated_on = record.updated_on,
                    total_records = record.total_records,
                    user_status_name = record.user_status_name

                }).ToList();

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;
            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public async Task<PageResponseRequest<AllUserDetailsUser>> GetUserExists(JwtTokenDetail userDetails)
        {
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.GetUserExists");

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            PageResponseRequest<AllUserDetailsUser> data = new PageResponseRequest<AllUserDetailsUser>();
            var response =await _userTrackerProvider.GetUserDetails(userDetails.Email);

            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int total_Pages = 1;
                var listResponse = response.Select(record => new AllUserDetailsUser()
                {
                    user_id = record.user_id,
                    user_first_name = record.user_first_name,
                    user_last_name = record.user_last_name,
                    user_email = record.user_email,
                    user_status = record.user_status,
                    user_role_id = record.user_role_id,
                    is_active = record.is_active,
                    is_deleted = record.is_deleted,
                    created_on = record.created_on,
                    updated_on = record.updated_on,
                    total_records = record.total_records,
                    whitelistdomain = record.white_list_domain


                }).ToList();

                data.Records = listResponse;
                data.TotalRecords = 1;
                data.TotalPages = total_Pages;
                data.PageSize = 1;
                data.CurrentPage = 1;
            }
            return data;
        }


        [ExcludeFromCodeCoverage]
        public DeleteUserResponse DeleteUser(DeleteUserRequest searchRequest, JwtTokenDetail userDetails)
        {      
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.DeleteUser");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            DeleteUserResponse data = new DeleteUserResponse();
            string response = _userTrackerProvider.DeleteUser(searchRequest, userDetails);

            if (response != null)
            {
                data.Message = response;
            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public UserActiveResponse UserActive(UserActiveRequest searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.UserActive");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            UserActiveResponse data = new UserActiveResponse();
            var response = _userTrackerProvider.UserActive(searchRequest , userDetails);

            if (response != null)
            {
                data.Message = response.Message;
            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public UserActiveResponse AddUpdateUser(AddUpdateUserRequest searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.AddUpdateUser");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            UserActiveResponse data = new UserActiveResponse();
            string response = _userTrackerProvider.AddUpdateUser(searchRequest , userDetails);

            if (response != null)
            {
                data.Message = response;
            }
            return data;
        }



        [ExcludeFromCodeCoverage]
        public PageResponseRequest<SummaryDetailsResponse> GetUserSummaryDetails(SearchRequestResponse<SummaryDetailsRequest> searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.GetUserSummaryDetails");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            PageResponseRequest<SummaryDetailsResponse> data = new PageResponseRequest<SummaryDetailsResponse>();
            var response = _userTrackerProvider.GetUserSummaryDetails(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                var listResponse = response.Select(e => new SummaryDetailsResponse()
                {
                    emp_code = e.emp_code,
                    total_active_time = e.total_active_time ?? 0,
                    total_application_count = e.total_application_count ?? 0,
                    total_call_duration = e.total_call_duration ?? 0,
                    total_idle_time = e.total_idle_time ?? 0,
                    total_call_count = e.total_call_count ?? 0,
                    total_browsing_count = e.total_browsing_count ?? 0,
                    total_browsing_time = e.total_browsing_time ?? 0,
                    total_location_count = e.total_location_count ?? 0,
                    total_login_count = e.total_login_count ?? 0,
                    EmployeeName = e.EmployeeName,
                    Location = e.Location,
                    total_application_count_time = e.total_application_time ?? 0,
                    OfficeLocation = e.OfficeLocation,
                }).ToList();
                data.Records = listResponse;
                data.TotalRecords = 0;
                data.TotalPages = 0;
                data.PageSize = 0;
                data.CurrentPage = 0;
            }
            return data;
        }


        [ExcludeFromCodeCoverage]
        public PageResponse<BrowsingHistoryResponse> GetBrowsingDetails(SearchRequest<BrowsingHistoryRequest> searchRequest, JwtTokenDetail userDetails)
        {
       
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.GetBrowsingDetails");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            PageResponse<BrowsingHistoryResponse> data = new PageResponse<BrowsingHistoryResponse>();
            var response = _userTrackerProvider.GetBrowsingDetails(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new BrowsingHistoryResponse()
                {
                   emp_code = e.emp_code,
                   application_name = e.application_name,
                   started_on = e.started_on,
                   ended_on = e.EndedOn,
                   description = e.description,
                   EmployeeName = e.EmployeeName


                }).ToList();
                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;
            }
            return data;
        }       

        [ExcludeFromCodeCoverage]
        public PageResponse<ApplicationTabDetailsResponse> GetRunningApplicationsDetailed(SearchRequest<ApplicationTabDetailsRequest> searchRequest, JwtTokenDetail userDetails)
        {
     
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.GetRunningApplicationsDetailed");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            PageResponse<ApplicationTabDetailsResponse> data = new PageResponse<ApplicationTabDetailsResponse>();
            var response = _userTrackerProvider.GetRunningApplicationsDetailed(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new ApplicationTabDetailsResponse()
                {
                    emp_code = e.emp_code,
                    ApplicationName = e.application_name,
                    StartedOn = e.started_on,
                    EndedOn = e.EndedOn,
                    description = e.description,
                    EmployeeName = e.EmployeeName,
                    TotalBrowsingTime = e.Total_Browsing_time,
                    CreatedOn = e.CreatedOn,
                    UpdatedOn = e.UpdatedOn
                }).ToList();

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;
            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public PageResponse<ApplicationTabResponse> GetApplicationTabDetails(SearchRequest<ApplicationTabRequest> searchRequest, JwtTokenDetail userDetails)
        {          
            _logger.LogInformation("Calling {ServiceMethod}", "UserInsightTrackerService.GetApplicationTabDetails");
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            PageResponse<ApplicationTabResponse> data = new PageResponse<ApplicationTabResponse>();
            var response = _userTrackerProvider.GetApplicationTabDetails(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new ApplicationTabResponse()
                {
                    emp_code = e.emp_code,
                    application_name = e.application_name,
                    StartedOn = e.StartedOn,
                    application_count = e.application_count,
                    browsing_time = e.browsing_time,
                    id = e.id,
                    EmployeeName = e.EmployeeName
                }).ToList();
                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;
            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public PageResponse<TopBottomDetailsResponseResult> GetSummaryTopBottomDetails(SummaryTopBottomDetails searchRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetSummaryTopBottomDetails called with {@SearchRequest}", searchRequest);
        
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
    
            PageResponse<TopBottomDetailsResponseResult> data = new PageResponse<TopBottomDetailsResponseResult>();
            var response = _userTrackerProvider.GetSummaryTopBottomDetails(searchRequest, userDetails);
            if (response == null || !response.Any())
                return null; // Return an empty list if no data

            var firstRecord = response.FirstOrDefault();
            var objApplicationActivityReportResult = new TopBottomDetailsResponseResult
            {
                SystemLoginDetailsTop10ActiveTime = new List<SystemLoginDetailsTop10ActiveTimeResult>(),
                SystemLoginDetailsBottom10ActiveTime = new List<SystemLoginDetailsTop10BottomTimeResult>(),

                SystemLoginDetailsTop10SleepTime = new List<SystemLoginDetailsTop10SleepTimeResult>(),
                SystemLoginDetailsBottom10SleepTime = new List<SystemLoginDetailsBottom10SleepTimeResult>(),
                SystemLoginDetailsTop10CallDurationTime = new List<SystemLoginDetailsTop10CallDurationResult>(),
                SystemLoginDetailsBottom10CallDurationTime = new List<SystemLoginDetailsBottom10CallDurationResult>()
            };

            SystemLogin(firstRecord, objApplicationActivityReportResult);
            if (firstRecord != null && firstRecord.SystemLoginDetailsTop10SleepTime != null)
            {

                // Populate SystemLoginDetailsTop10SleepTime
                foreach (var record in firstRecord.SystemLoginDetailsTop10SleepTime)
                {
                    var sleeptopResult = new SystemLoginDetailsTop10SleepTimeResult
                    {
                        EmpCode = record.EmpCode,
                        loginDate = record.loginDate,
                        Top10_IdleTime = record.Top10_IdleTime,
                        EmployeeName = record.EmployeeName
                    };
                    objApplicationActivityReportResult.SystemLoginDetailsTop10SleepTime.Add(sleeptopResult);
                }
            }
            SystemLoginDetails(firstRecord, objApplicationActivityReportResult);
            if (firstRecord != null && firstRecord.SystemLoginDetailsBottom10CallDurationTime != null)
            {
                // Populate SystemLoginDetailsBottom10CallDurationTime
                foreach (var record in firstRecord.SystemLoginDetailsBottom10CallDurationTime)
                {
                    var bottom10calldurationResult = new SystemLoginDetailsBottom10CallDurationResult
                    {
                        EmpCode = record.EmpCode,
                        StartDate = record.StartDate,
                        CallsCount = record.CallsCount,
                        Bottom10_Call_Duration = record.Bottom10_Call_Duration,
                        EmployeeName = record.EmployeeName
                    };
                    objApplicationActivityReportResult.SystemLoginDetailsBottom10CallDurationTime.Add(bottom10calldurationResult);
                }
            }
            // Add the result to the list and assign it to the Records
            data.Records = new List<TopBottomDetailsResponseResult> { objApplicationActivityReportResult };

            return data;
        }
        

            [ExcludeFromCodeCoverage]
        private static void SystemLoginDetails(TopBottomDetailsResponseResult firstRecord, TopBottomDetailsResponseResult objApplicationActivityReportResult)
        {
            if (firstRecord != null && firstRecord.SystemLoginDetailsBottom10SleepTime != null)
            {
                // Populate SystemLoginDetailsBottom10SleepTime
                foreach (var record in firstRecord.SystemLoginDetailsBottom10SleepTime)
                {
                    var bottomsleepResult = new SystemLoginDetailsBottom10SleepTimeResult
                    {
                        EmpCode = record.EmpCode,
                        loginDate = record.loginDate,
                        Bottom10_IdleTime = record.Bottom10_IdleTime,
                        EmployeeName = record.EmployeeName
                    };
                    objApplicationActivityReportResult.SystemLoginDetailsBottom10SleepTime.Add(bottomsleepResult);
                }
            }
            if (firstRecord != null && firstRecord.SystemLoginDetailsTop10CallDurationTime != null)
            {

                // Populate SystemLoginDetailsTop10CallDurationTime
                foreach (var record in firstRecord.SystemLoginDetailsTop10CallDurationTime)
                {
                    var top10calldurationResult = new SystemLoginDetailsTop10CallDurationResult
                    {
                        EmpCode = record.EmpCode,
                        StartDate = record.StartDate,
                        CallsCount = record.CallsCount,
                        Top10_Call_Duration = record.Top10_Call_Duration,
                        EmployeeName = record.EmployeeName
                    };
                    objApplicationActivityReportResult.SystemLoginDetailsTop10CallDurationTime.Add(top10calldurationResult);
                }
            }
        }

        [ExcludeFromCodeCoverage]
        private static void SystemLogin(TopBottomDetailsResponseResult firstRecord, TopBottomDetailsResponseResult objApplicationActivityReportResult)
        {
            if (firstRecord != null && firstRecord.SystemLoginDetailsTop10ActiveTime != null)
            {
                foreach (var record in firstRecord.SystemLoginDetailsTop10ActiveTime)
                {
                    var activeResult = new SystemLoginDetailsTop10ActiveTimeResult
                    {
                        EmpCode = record.EmpCode,
                        loginDate = record.loginDate,
                        Top10_ActiveTime = record.Top10_ActiveTime,
                        EmployeeName = record.EmployeeName
                    };
                    objApplicationActivityReportResult.SystemLoginDetailsTop10ActiveTime.Add(activeResult);
                }
            }

            if (firstRecord != null && firstRecord.SystemLoginDetailsBottom10ActiveTime != null)
            {
                // Populate BottomTime results (if applicable)
                foreach (var record in firstRecord.SystemLoginDetailsBottom10ActiveTime)
                {
                    var bottomResult = new SystemLoginDetailsTop10BottomTimeResult
                    {
                        EmpCode = record.EmpCode,
                        loginDate = record.loginDate,
                        Bottom10_ActiveTime = record.Bottom10_ActiveTime,
                        EmployeeName = record.EmployeeName
                    };
                    objApplicationActivityReportResult.SystemLoginDetailsBottom10ActiveTime.Add(bottomResult);
                }
            }
        }
        [ExcludeFromCodeCoverage]
        public PageResponse<LocationDetailsResponse> GetLocationDetails(SearchRequest<LocationDetailsRequest> searchRequest, JwtTokenDetail userDetails)
        {
           _logger.LogInformation("UserInsightTrackerService.GetLocationDetails called with {@SearchRequest}", searchRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<LocationDetailsResponse> data = new PageResponse<LocationDetailsResponse>();
            var response = _userTrackerProvider.GetLocationDetails(searchRequest, userDetails);
            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.Total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.Total_Pages);

                var listResponse = response.Select(e => new LocationDetailsResponse()
                {
                  Location = e.Location,
                  emp_code = e.emp_code,
                  is_vpn_connected = e.is_vpn_connected,
                  Id = e.Id,
                  latitude = e.latitude,
                  longitude = e.longitude,
                  updated_on = e.updated_on,
                  StaffLineLocation = e.StaffLineLocation

                }).ToList();

                if (searchRequest.PageNumber > total_Pages && total_Pages > 0)
                    searchRequest.PageNumber = total_Pages;

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = searchRequest.PageSize;
                data.CurrentPage = searchRequest.PageNumber;

            }
            return data;
        }

        [ExcludeFromCodeCoverage]
        public List<DataContracts.PermissionResponseResult> GetPermissionDetails(PermissionDetailRequest permissionDetailRequest, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetPermissionDetails called with {@PermissionDetailRequest}", permissionDetailRequest);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            var response = _userTrackerProvider.GetPermissionDetails(permissionDetailRequest, userDetails);

            // Create a list of PermissionResponseResult
            var permissionResponseResultList = new List<DataContracts.PermissionResponseResult>();

            if (response.Any())
            {
                // Map the response data into PermissionResponseResult
                var permissionResponseResult = new DataContracts.PermissionResponseResult
                {
                    Records = response.Select(record => new DataContracts.PermissionResponse
                    {
                        MenuName = record.MenuName,
                        SubMenuName = record.SubMenuName,
                        Path = record.Path,
                        Access = record.Access.Split(",").ToList()
                    }).ToList()
                };

                // Add the created object to the list
                permissionResponseResultList.Add(permissionResponseResult);
            }

            return permissionResponseResultList;
        }



        [ExcludeFromCodeCoverage]

        public PageResponse<ManagerRolesResponse> GetManagerRoles(ManagerRolesRequest managerRoles, JwtTokenDetail userDetails)
        {
          
            _logger.LogInformation("UserInsightTrackerService.GetManagerRoles called with {@managerRoles}", managerRoles);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<ManagerRolesResponse> data = new PageResponse<ManagerRolesResponse>();
            var response = _userTrackerProvider.GetManagerRoles(managerRoles, userDetails);

            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                int totalRecords = Convert.ToInt32(firstRecord.total_Records);
                int total_Pages = Convert.ToInt32(firstRecord.total_Pages);
                var listResponse = response.Select(e => new ManagerRolesResponse()
                {
                    RoleName = e.RoleName,
                    Status = e.Status,
                    Role_Id = e.role_id
                }).ToList();

                data.Records = listResponse;
                data.TotalRecords = totalRecords;
                data.TotalPages = total_Pages;
                data.PageSize = 1;
                data.CurrentPage = 1;
            }
            return data;

        }



        [ExcludeFromCodeCoverage]
        public PageResponse<RoleDetailsResponse> GetRoleDetails(RoleDetailsRequest roleDetails, JwtTokenDetail userDetails)
        {
            _logger.LogInformation("UserInsightTrackerService.GetManagerRoles called with {@RoleDetailsRequest}", roleDetails);

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<RoleDetailsResponse> data = new PageResponse<RoleDetailsResponse>();

            var response = _userTrackerProvider.GetRoleDetails(roleDetails, userDetails);

            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                var listResponse = response.Select(e => new RoleDetailsResponse()
                {
                    RoleName = e.Role_Name,
                    SubFeatureName = e.SubFeature_Name,
                    Status = e.Status,
                    PermissionNames = e.Permission_Names,
                    FeatureSubFeature_Id = e.FeatureSubFeature_Id,
                    Permission_Id=e.permission_id,
                    Role_Id=e.Role_Id,
                    Path = e.Path,
                    Menu_Name = e.Menu_Name,
                    ViewType = e.ViewType,
                   
                   
                }).ToList();

                data.Records = listResponse;
                data.TotalPages = 1;
                data.CurrentPage = 1;
            }
            return data;
        }


        [ExcludeFromCodeCoverage]
        public PageResponse<CreateRolesResponse> CreateOrModifyRoles(CreateRolesRequest createRoles, JwtTokenDetail userDetails)
        {   
            _logger.LogInformation("UserInsightTrackerService.CreateOrModifyRoles called with {@CreateRolesRequest}", createRoles); 

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<CreateRolesResponse> data = new PageResponse<CreateRolesResponse>();
            var response = _userTrackerProvider.CreateOrModifyRoles(createRoles, userDetails);

            var firstRecord = response.FirstOrDefault();
            if (firstRecord != null)
            {
                var listResponse = response.Select(e => new CreateRolesResponse()
                {
                    Message = e.message,

                }).ToList();

                data.Records = listResponse;
            }
            return data;

        }

        [ExcludeFromCodeCoverage]
        public PageResponse<UpdateRoleResponse> UpdateRolePermission(List<UpdateRoleRequest> roleRequest, JwtTokenDetail userDetails)
        {

            _logger.LogInformation("UserInsightTrackerService.CreateOrModifyRoles called with {@UpdateRoleRequest}", roleRequest); 

            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));

            PageResponse<UpdateRoleResponse> data = new PageResponse<UpdateRoleResponse>();
            var allResponses = new List<UpdateRoleResponse>();

            foreach (var roleRequests in roleRequest)  // Loop through list
            {
                var response = _userTrackerProvider.UpdateRolePermission(roleRequests, userDetails);
                var listResponse = response.Select(e => new UpdateRoleResponse()
                {
                    Message = e.message,
                    RoleId = e.role_id,
                    RoleName = e.role_name,
                }).ToList();

                allResponses.AddRange(listResponse);
            }

            data.Records = allResponses;
            data.CurrentPage = 1;
            data.TotalRecords = 1;
            data.TotalPages = 1;
            data.PageSize = 1;
            return data;
        }



        [ExcludeFromCodeCoverage]
        public PageResponse<ApplicationPropertyResponse> GetAllApplication(JwtTokenDetail userDetails)
        {
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<ApplicationPropertyResponse> data = new PageResponse<ApplicationPropertyResponse>();
            var response = _userTrackerProvider.GetAllApplication(userDetails);
            if (response != null)
            {
                int totalRecords = response.Count;

                var listResponse = response.Select(e => new ApplicationPropertyResponse()
                {
                    Id = e.id,
                    ClientName = e.client_name,
                    Profile = e.profile,
                    ProKey = e.pro_key,
                    ProValue = e.pro_value,
                    UpdatedOn = e.updated_on,
                    CreatedOn = e.created_on,

                }).ToList();

                data.Records = listResponse;
                data.CurrentPage = 1; // Set to current page if pagination is needed
                data.PageSize = 1; // Set appropriate page size
                data.TotalRecords = totalRecords;
                data.TotalPages = 1; // Fix: Use the correct variable

            }

            return data;
        }

        [ExcludeFromCodeCoverage]
        public PageResponse<MenuListResponse> GetAllMenu(JwtTokenDetail userDetails)
        {
            if (userDetails == null)
                throw new UnauthorisedException(_utils.GetMessage("UserInvalid"));
            PageResponse<MenuListResponse> data = new PageResponse<MenuListResponse>();
            var response = _userTrackerProvider.GetAllMenu(userDetails);
            if (response != null)
            {
                int totalRecords = response.Count;

                var listResponse = response.Select(e => new MenuListResponse()
                {
                    Id = e.Id,
                    Name = e.Name,
                    Path = e.Path,
                    IsActive = e.IsActive,
                
                }).ToList();

                data.Records = listResponse;
                data.CurrentPage = 1; // Set to current page if pagination is needed
                data.PageSize = 1; // Set appropriate page size
                data.TotalRecords = totalRecords;
                data.TotalPages = 1; // Fix: Use the correct variable

            }

            return data;
        }
    }
}