<?php
include_once 'config/dbConfig.php';
include_once 'functions/sheduler.php';
require 'controller/controllerPage.php';

$pageDetails = new pageViewer();
session_start();
if (!isset($_SESSION['seenSwipeMessage'])) {
    $showSwipeMessage = true;
} else {
    $showSwipeMessage = false;
}
header('Cache-Control: public, max-age=86400');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');
?>

<!DOCTYPE html>
<html lang="en">
	<head>
        <?php include_once 'support/seoHelmet.php'; ?>
		<?php include_once 'support/head.php'; ?>
	</head>
	<body class="mood-other">
        <?php include_once 'support/navBar.php' ?>
		<?= $pageDetails->includePage();?>
		<?php include_once 'support/footer.php'; ?>
	</body>
</html>

<?php if ($showSwipeMessage): ?>
<div id="swipeMessageOverlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.8); z-index: 999999; color: white; justify-content: center; align-items: center;">
    <div style="text-align: center;">
		<img src="img/swipe.png"><br>
        <h3 class="mt-2" style="font-size: 15px;font-weight: bold">Swipe Left or Right to Change Pages</h3>
        <p style="margin-top: 10px;">Use a swipe gesture to navigate through the pages.</p>
        <button id="closeSwipeMessage" class="btn btn-primary" style="margin-top: 20px;">Got it <i class="fa-solid fa-arrow-right"></i></button>
    </div>
</div>
<?php endif; ?>

<script>
    document.addEventListener("DOMContentLoaded", function () {
        var isMobile = window.innerWidth <= 768;
        if (isMobile) {
            var closeSwipeMessage = document.getElementById('closeSwipeMessage');
            if (closeSwipeMessage) {
                closeSwipeMessage.addEventListener('click', function () {
                    var swipeMessageOverlay = document.getElementById('swipeMessageOverlay');
                    swipeMessageOverlay.style.display = 'none';
                    var xhr = new XMLHttpRequest();
                    xhr.open("POST", "functions/session.php", true);
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xhr.send("seenSwipeMessage=true");
                });
            }
        }
    });
</script>

<?php
$file = $rootPath . '/assets/public/visitors/views.json';

// Load existing data if the file exists
$data = [];
if (file_exists($file)) {
    $jsonContent = file_get_contents($file);
    $data = json_decode($jsonContent, true) ?? []; // Use empty array if JSON is invalid
}

$currentYear = date('Y');
$currentMonth = date('m');
$newViews = 1; // Increment view by 1 or your specified count

// Update views for the current month
$data[$currentYear][$currentMonth] = ($data[$currentYear][$currentMonth] ?? 0) + $newViews;

// Remove data older than 12 months
$monthsToKeep = 12;
foreach ($data as $year => $months) {
    foreach ($months as $month => $views) {
        $yearInt = (int)$year;
        $monthInt = (int)$month;
        $monthDifference = (($currentYear - $yearInt) * 12) + ($currentMonth - $monthInt);

        if ($monthDifference >= $monthsToKeep) {
            unset($data[$year][$month]);
            if (empty($data[$year])) {
                unset($data[$year]);
            }
        }
    }
}

// Write updated data back to the file
$jsonData = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents($file, $jsonData, LOCK_EX);
?>

<?php
mysqli_close($con);
?>