// HTML-Generator für nuscore lokale Kopie
export function generateNuscoreHTML(scripts, stylesheets, useExternalUrls = false) {
  const parts = [];
  
  parts.push('<!DOCTYPE html>');
  parts.push('<html lang="de">');
  
  // Head
  parts.push('<head>');
  parts.push('    <meta charset="UTF-8">');
  parts.push('    <meta name="viewport" content="width=device-width, initial-scale=1.0">');
  parts.push('    <title>nuscore - Lokale Kopie</title>');
  parts.push('    <base href="/">');
  parts.push('    ');
  parts.push('    <!-- Stylesheets -->');
  
  stylesheets.forEach(sheet => {
    const href = useExternalUrls ? sheet.url : sheet.localUrl;
    parts.push(`    <link rel="stylesheet" href="${href}">`);
  });
  
  parts.push('    ');
  parts.push('    <!-- PIN-Einfügung Script -->');
  parts.push('    <script>');
  parts.push('        window.addEventListener("message", function(event) {');
  parts.push('            if (event.data && event.data.action === "fillPin") {');
  parts.push('                console.log("PIN-Einfügung empfangen:", event.data.pin);');
  parts.push('                ');
  parts.push('                const pinSelectors = [');
  parts.push('                    "input[type=\\"password\\"][placeholder*=\\"Vereins\\"]",');
  parts.push('                    "input[type=\\"password\\"][placeholder*=\\"Spiel-Pin\\"]",');
  parts.push('                    "input[type=\\"password\\"][placeholder*=\\"PIN\\"]",');
  parts.push('                    "input[type=\\"password\\"]"');
  parts.push('                ];');
  parts.push('                ');
  parts.push('                let pinField = null;');
  parts.push('                for (const selector of pinSelectors) {');
  parts.push('                    pinField = document.querySelector(selector);');
  parts.push('                    if (pinField) break;');
  parts.push('                }');
  parts.push('                ');
  parts.push('                if (pinField) {');
  parts.push('                    pinField.value = event.data.pin;');
  parts.push('                    pinField.dispatchEvent(new Event("input", { bubbles: true }));');
  parts.push('                    pinField.dispatchEvent(new Event("change", { bubbles: true }));');
  parts.push('                    console.log("PIN erfolgreich eingefügt:", event.data.pin);');
  parts.push('                }');
  parts.push('            }');
  parts.push('        });');
  parts.push('    </script>');
  parts.push('</head>');
  
  // Body
  parts.push('<body>');
  parts.push('    <app-root></app-root>');
  parts.push('    ');
  parts.push('    <!-- Scripts -->');
  
  scripts.forEach(script => {
    const src = useExternalUrls ? script.url : script.localUrl;
    parts.push(`    <script src="${src}"></script>`);
  });
  
  parts.push('    ');
  parts.push('    <!-- Initialisierung -->');
  parts.push('    <script>');
  parts.push('        console.log("nuscore lokale Kopie gestartet");');
  parts.push('        console.log("PIN-Einfügung bereit");');
  parts.push('        ');
  parts.push('        const urlParams = new URLSearchParams(window.location.search);');
  parts.push('        const code = urlParams.get("code");');
  parts.push('        if (code) {');
  parts.push('            console.log("Code aus URL gefunden:", code);');
  parts.push('        }');
  parts.push('        ');
  parts.push('        // Angular Router Konfiguration für lokale Kopie');
  parts.push('        if (window.ng && window.ng.core) {');
  parts.push('            console.log("Angular gefunden, konfiguriere Router...");');
  parts.push('        }');
  parts.push('    </script>');
  parts.push('</body>');
  parts.push('</html>');
  
  return parts.join('\n');
}
