from base64 import b64encode
import json
from pathlib import Path

header = '''<!doctype html>
<html lang="en" data-server="disabled" data-launch="public">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="theme-color" content="#F1F8F5">
<meta name="description" content="Scalping Space review demo for a trading call and subscriber community website.">
<title>Scalping Space | Trading desk</title>
<link rel="icon" type="image/svg+xml" href="FAVICON_DATA">
<style>
'''
body = '''
</style>
</head>
<body>
<div id="app"><main class="page">Loading Scalping Space…</main></div>
<noscript><p class="page">Enable JavaScript to use the interactive demo.</p></noscript>
<div id="toast" class="toast hidden" role="status" aria-live="polite"></div>
<dialog id="dialog" class="dialog" aria-labelledby="dialog-title">
<div class="sectionHead"><h2 id="dialog-title"></h2><button type="button" class="btn outline buttonSmall" data-action="close" aria-label="Close dialog">Close</button></div>
<div id="dialog-body"></div>
</dialog>
<script id="desk-core">
'''
academy_art = b64encode(Path('assets/academy.webp').read_bytes()).decode('ascii')
theme = Path('theme.css').read_text().replace('ACADEMY_ART_BASE64', academy_art)
def svg_data(filename):
    return 'data:image/svg+xml;base64,' + b64encode(Path('../brand', filename).read_bytes()).decode('ascii')

header = header.replace('FAVICON_DATA', svg_data('scalping-space-mark.svg'))
brand_js = f'const scalpingWordmark = {svg_data("scalping-space-light.svg")!r};\n'
def bundle_qr():
    # Adapt the vendored CommonJS QR encoder for the standalone HTML.
    modules = []
    for path in sorted(Path('vendor/QRCode').glob('*.js')):
        modules.append(f'{path.stem!r}:(module,require)=>{{\n{path.read_text()}\n}}')
    registry = ',\n'.join(modules)
    return 'const UpiQrEncoder=(()=>{const modules={' + registry + '''};
    const cache={};
    function load(path){
      const key=path.startsWith('./')?path.slice(2):path;
      if(!cache[key]){const module={exports:{}};modules[key](module,load);cache[key]=module.exports;}
      return cache[key];
    }
    return load('index');
    })();\n'''

demo_docs = {path.name: b64encode(path.read_bytes()).decode('ascii') for path in sorted(Path('../app/academy').glob('*.pdf'))}
demo_docs['sample-lesson.mp4'] = b64encode(Path('assets/academy-demo-lesson.mp4').read_bytes()).decode('ascii')
doc_script = '\n<script id="academy-samples">const academyDemoFiles=' + json.dumps(demo_docs) + ';</script>\n'

out = (header + theme + body + Path('portfolio-core.js').read_text() + '\n' + Path('commerce-core.js').read_text() + '\n' + Path('market-core.js').read_text() + '\n' + Path('settings-core.js').read_text() + '\n' + Path('academy-core.js').read_text() + '\n' + Path('core.js').read_text()
       + '</script>' + doc_script + '<script id="desk-ui">\n' + bundle_qr() + brand_js + Path('site.js').read_text() + '\n' + Path('chat.js').read_text() + '\n' + Path('portfolio.js').read_text() + '\n' + Path('auth-ui.js').read_text() + '\n' + Path('commerce-ui.js').read_text() + '\n' + Path('academy-ui.js').read_text() + '\n' + Path('market-ui.js').read_text() + '\n' + Path('settings-ui.js').read_text() + '\n' + Path('ui.js').read_text() + '</script>\n</body>\n</html>\n')
Path('../app/Scalping-Space-Review.html').write_text(out)
Path('../dist/index.html').write_text(out)
