<?php
declare(strict_types=1);

require_once __DIR__ . '/api/admin_gate.php';
mocker_admin_panel_html_gate();

header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');

$scriptName = $_SERVER['SCRIPT_NAME'] ?? '/admin.php';
$webDir = dirname(str_replace('\\', '/', $scriptName));
if ($webDir === '/' || $webDir === '.' || $webDir === '\\') {
    $webDir = '';
} else {
    $webDir = rtrim($webDir, '/');
}
$adminCssPath = __DIR__ . '/admin/admin.css';
$adminCssHref = ($webDir === '' ? '' : $webDir) . '/admin/admin.css';
$adminCssHref .= is_file($adminCssPath) ? '?v=' . filemtime($adminCssPath) : '';
$adminJsPath = __DIR__ . '/admin/admin.js';
$adminJsHref = ($webDir === '' ? '' : $webDir) . '/admin/admin.js';
$adminJsHref .= is_file($adminJsPath) ? '?v=' . filemtime($adminJsPath) : '';
$tailwindCssPath = __DIR__ . '/css/app.css';
$tailwindCssHref = ($webDir === '' ? '' : $webDir) . '/css/app.css';
$tailwindCssHref .= is_file($tailwindCssPath) ? '?v=' . filemtime($tailwindCssPath) : '';

?><!DOCTYPE html>
<html lang="tr" data-admin-theme="light">
<head>
  <meta charset="UTF-8" />
  <script>
    (function () {
      var key = "mocker-admin-theme";
      var theme = "light";
      try {
        var saved = localStorage.getItem(key);
        if (saved === "dark" || saved === "light") theme = saved;
        else if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) theme = "dark";
      } catch (e) {}
      document.documentElement.setAttribute("data-admin-theme", theme);
      document.documentElement.style.colorScheme = theme;
      var meta = document.querySelector('meta[name="theme-color"]');
      if (meta) meta.setAttribute("content", theme === "dark" ? "#181f2e" : "#ffffff");
    })();
  </script>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="robots" content="noindex, nofollow" />
  <meta name="theme-color" content="#eef2f8" />
  <meta name="color-scheme" content="light dark" />
  <meta name="description" content="Mocker yönetim konsolu — içerik, üyeler ve özet." />
  <meta name="mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="default" />
  <title>Mocker — Yönetim</title>
  <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>" />
  <script>
    (function () {
      var blank = "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>";
      document.querySelectorAll("link[rel]").forEach(function (el) {
        var r = (el.getAttribute("rel") || "").toLowerCase();
        if (r.indexOf("icon") !== -1 || r.indexOf("apple-touch") !== -1) el.remove();
      });
      var link = document.createElement("link");
      link.rel = "icon";
      link.href = blank;
      document.head.appendChild(link);
    })();
  </script>
  <link rel="preconnect" href="https://fonts.googleapis.com" />
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link rel="dns-prefetch" href="https://cdn.jsdelivr.net" />
  <link
    rel="stylesheet"
    href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,400;0,9..40,500;0,9..40,600;0,9..40,700;1,9..40,400&family=Plus+Jakarta+Sans:ital,wght@0,500;0,600;0,700;1,500&display=swap"
  />
  <?php
  $adminLightCssPath = __DIR__ . '/admin/admin-light.css';
  $adminLightCssHref = ($webDir === '' ? '' : $webDir) . '/admin/admin-light.css';
  $adminLightCssHref .= is_file($adminLightCssPath) ? '?v=' . filemtime($adminLightCssPath) : '';
  $adminDarkCssPath = __DIR__ . '/admin/admin-dark.css';
  $adminDarkCssHref = ($webDir === '' ? '' : $webDir) . '/admin/admin-dark.css';
  $adminDarkCssHref .= is_file($adminDarkCssPath) ? '?v=' . filemtime($adminDarkCssPath) : '';
  $adminThemeCssPath = __DIR__ . '/admin/admin-theme.css';
  $adminThemeCssHref = ($webDir === '' ? '' : $webDir) . '/admin/admin-theme.css';
  $adminThemeCssHref .= is_file($adminThemeCssPath) ? '?v=' . filemtime($adminThemeCssPath) : '';
  $adminTicketsCssPath = __DIR__ . '/admin/admin-tickets.css';
  $adminTicketsCssHref = ($webDir === '' ? '' : $webDir) . '/admin/admin-tickets.css';
  $adminTicketsCssHref .= is_file($adminTicketsCssPath) ? '?v=' . filemtime($adminTicketsCssPath) : '';
  $adminPanelsCssPath = __DIR__ . '/admin/admin-panels.css';
  $adminPanelsCssHref = ($webDir === '' ? '' : $webDir) . '/admin/admin-panels.css';
  $adminPanelsCssHref .= is_file($adminPanelsCssPath) ? '?v=' . filemtime($adminPanelsCssPath) : '';
  $adminCmsCssPath = __DIR__ . '/admin/admin-cms.css';
  $adminCmsCssHref = ($webDir === '' ? '' : $webDir) . '/admin/admin-cms.css';
  $adminCmsCssHref .= is_file($adminCmsCssPath) ? '?v=' . filemtime($adminCmsCssPath) : '';
  ?>
  <link rel="stylesheet" href="<?php echo htmlspecialchars($tailwindCssHref, ENT_QUOTES, 'UTF-8'); ?>" />
  <link rel="stylesheet" href="<?php echo htmlspecialchars($adminCssHref, ENT_QUOTES, 'UTF-8'); ?>" />
  <link rel="stylesheet" href="<?php echo htmlspecialchars($adminLightCssHref, ENT_QUOTES, 'UTF-8'); ?>" />
  <link rel="stylesheet" href="<?php echo htmlspecialchars($adminDarkCssHref, ENT_QUOTES, 'UTF-8'); ?>" />
  <link rel="stylesheet" href="<?php echo htmlspecialchars($adminThemeCssHref, ENT_QUOTES, 'UTF-8'); ?>" />
  <link rel="stylesheet" href="<?php echo htmlspecialchars($adminTicketsCssHref, ENT_QUOTES, 'UTF-8'); ?>" />
  <link rel="stylesheet" href="<?php echo htmlspecialchars($adminPanelsCssHref, ENT_QUOTES, 'UTF-8'); ?>" />
  <link rel="stylesheet" href="<?php echo htmlspecialchars($adminCmsCssHref, ENT_QUOTES, 'UTF-8'); ?>" />
  <?php
  $apiBaseHref = ($webDir === '' ? '' : $webDir) . '/api/';
  ?>
  <script>
    window.__ADMIN_API_BASE__ = <?php echo json_encode($apiBaseHref, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
  </script>
</head>
<body class="admin-body antialiased">
  <div id="app">
    <div id="admin-boot" class="admin-boot" role="status" aria-live="polite" aria-busy="true">
      <div class="admin-boot__card">
        <div class="admin-boot__mark" aria-hidden="true">A</div>
        <p class="admin-boot__brand">Mocker</p>
        <p class="admin-boot__hint">Yönetim paneli yükleniyor</p>
        <div class="admin-boot__track" aria-hidden="true"><span class="admin-boot__bar"></span></div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.2/Sortable.min.js"></script>
  <script type="module" src="<?php echo htmlspecialchars($adminJsHref, ENT_QUOTES, 'UTF-8'); ?>"></script>
</body>
</html>
