<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Generate pages.json in /assets</title>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      font-family: 'Courier New', Courier, monospace;
      background-color: #2e2e2e;
      color: #d4d4d4;
      padding: 20px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      min-height: 100vh;
      flex-direction: column;
    }

    h2 {
      font-size: 24px;
      color: #61dafb;
      margin-bottom: 20px;
      text-align: center;
    }

    button {
      background-color: #4CAF50;
      color: #fff;
      padding: 14px 28px;
      font-size: 13px;
      font-weight: bold;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s;
      width: 100%;
      max-width: 300px;
      margin-top: 20px;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }

    button:hover {
      background-color: #269ccc;
    }

    #log {
      background-color: #1e1e1e;
      color: #dcdcdc;
      padding: 20px;
      border-radius: 6px;
      border: 1px solid #444;
      font-size: 14px;
      margin-bottom: 20px;
      width: 100%;
      max-width: 700px;
      white-space: pre-wrap;
      overflow-y: auto;
      height: 180px;
    }

    #log.success {
      color: #4CAF50;
    }

    #log.error {
      color: #f44336;
    }

    pre {
      background-color: #1e1e1e;
      color: #dcdcdc;
      padding: 20px;
      border-radius: 6px;
      border: 1px solid #444;
      font-size: 14px;
      overflow-x: auto;
      white-space: pre-wrap;
      word-wrap: break-word;
      margin-top: 20px;
      width: 100%;
      max-width: 700px;
    }

    #restartBtn {
      background-color: #f44336;
    }

    #restartBtn:hover {
      background-color: #e53935;
    }
  </style>
</head>
<body>

  <h2>Generate pages.json in /assets</h2>
  <div id="log"></div>
  <pre id="jsonOutput"></pre>
  <button id="generateBtn">Select Root Folder & Generate</button>
  <button id="restartBtn" style="display:none;">Restart</button>

  <script>
    async function generatePagesJson() {
      const log = document.getElementById("log");
      const jsonOutput = document.getElementById("jsonOutput");
      const restartBtn = document.getElementById("restartBtn");

      log.textContent = "Opening root folder picker...";

      try {
        const rootDir = await window.showDirectoryPicker();
        log.textContent = "Root folder selected. Checking for 'assets' folder...";

        let assetsDir;
        try {
          assetsDir = await rootDir.getDirectoryHandle("assets", { create: false });
        } catch {
          log.textContent = "❌ The 'assets' folder is not found. Creating it automatically...";
          assetsDir = await rootDir.getDirectoryHandle("assets", { create: true });
          log.textContent += "\n✅ 'assets' folder has been created.";
        }

        const htmlFiles = [];

        async function scanDir(dirHandle, path = "") {
          for await (const entry of dirHandle.values()) {
            const fullPath = path ? `${path}/${entry.name}` : entry.name;

            if (entry.kind === "directory") {
              if (["node_modules", ".git"].includes(entry.name)) continue;
              await scanDir(entry, fullPath);
            } else if (entry.name.endsWith(".html")) {
              htmlFiles.push(fullPath);
            }
          }
        }

        await scanDir(rootDir);
        htmlFiles.sort(); // Alphabetically sort

        const fileHandle = await assetsDir.getFileHandle("pages.json", { create: true });
        const writable = await fileHandle.createWritable();
        await writable.write(JSON.stringify(htmlFiles, null, 2));
        await writable.close();

        log.innerHTML = `<span class="success">✅ pages.json has been created/updated in /assets with ${htmlFiles.length} HTML files.</span>`;
        jsonOutput.textContent = JSON.stringify(htmlFiles, null, 2);
        restartBtn.style.display = "inline-block";

      } catch (err) {
        log.innerHTML = `<span class="error">❌ Error: ${err.message}</span>`;
        restartBtn.style.display = "inline-block";
      }
    }

    document.getElementById("generateBtn").addEventListener("click", () => {
      generatePagesJson();
    });

    document.getElementById("restartBtn").addEventListener("click", () => {
      document.getElementById("log").textContent = "";
      document.getElementById("jsonOutput").textContent = "";
      document.getElementById("restartBtn").style.display = "none";
      document.getElementById("generateBtn").disabled = false;
      generatePagesJson();
    });
  </script>
</body>
</html>
