<!doctype html>
<html lang="ru">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>MailHook запускается</title>
  <style>
    body{font-family:Arial,sans-serif;background:#0f172a;color:#e2e8f0;display:flex;align-items:center;justify-content:center;height:100vh;margin:0}
    .box{max-width:620px;padding:24px;border:1px solid #334155;border-radius:12px;background:#111827}
    .muted{color:#94a3b8;font-size:14px}
    .ok{color:#22c55e}
  </style>
</head>
<body>
  <div class="box">
    <h2>Сервис запускается</h2>
    <p class="muted">Проверяем доступность каждые 5 сек...</p>
    <p id="s" class="muted">Статус: ожидание</p>
  </div>

  <script>
    (function () {
      var startedAt = Date.now();
      var maxWaitMs = 180000;
      var intervalMs = 5000;
      var target = window.location.pathname + window.location.search + window.location.hash;
      var statusEl = document.getElementById("s");

      function check() {
        fetch(window.location.origin + "/api/auth/status", {
          method: "GET",
          cache: "no-store",
          credentials: "same-origin"
        })
        .then(function (r) {
          if (r.ok) {
            statusEl.textContent = "Статус: сервис доступен, открываем...";
            statusEl.className = "ok";
            window.location.replace(target || "/");
            return;
          }
          statusEl.textContent = "Статус: backend отвечает " + r.status + ", ждем...";
        })
        .catch(function () {
          statusEl.textContent = "Статус: backend еще не поднялся...";
        })
        .finally(function () {
          if (Date.now() - startedAt < maxWaitMs) {
            setTimeout(check, intervalMs);
          } else {
            statusEl.textContent = "Долго не отвечает. Нажмите Ctrl+F5 или проверьте контейнер.";
          }
        });
      }

      setTimeout(check, 1200);
    })();
  </script>
</body>
</html>