"""Build editor.html template from vvvebjs source with Jinja2 escaping."""
from pathlib import Path

BASE = Path(__file__).parent
SRC = BASE / "static" / "Vvvebjs" / "editor.html"
OUT = BASE / "templates" / "editor.html"

content = SRC.read_text(encoding="utf-8")

# ── Normalize line endings to LF to avoid CRLF mismatch on Windows ──
content = content.replace("\r\n", "\n")

# ── Correct themeBaseUrl and mediaPath in editor.html ──
content = content.replace("Vvveb.themeBaseUrl = 'demo/landing/';", "Vvveb.themeBaseUrl = '/static/Vvvebjs/demo/landing/';")
content = content.replace("window.mediaPath = '../../media';", "window.mediaPath = '/static/Vvvebjs/media/';")

# ── 1. Fix static asset paths ────────────────────────────────────
VBASE = "/static/Vvvebjs/"
replacements = [
    ('href="css/', f'href="{VBASE}css/'),
    ('href="libs/', f'href="{VBASE}libs/'),
    ('href="img/', f'href="{VBASE}img/'),
    ('href="favicon.ico"', f'href="{VBASE}favicon.ico"'),
    ('src="libs/', f'src="{VBASE}libs/'),
    ('src="js/', f'src="{VBASE}js/'),
    ('src="img/', f'src="{VBASE}img/'),
    ('src="media/', f'src="{VBASE}media/'),
    ('src="fonts/', f'src="{VBASE}fonts/'),
    ('src="demo/', f'src="{VBASE}demo/'),
]
for old, new in replacements:
    content = content.replace(old, new)

# ── 2. Fix PHP save URL → Flask API ─────────────────────────────
content = content.replace('data-vvveb-url="save.php"', 'data-vvveb-url="/api/save"')

# ── 3. Fix JS url variables ──────────────────────────────────────
JS_VARS = [
    ("let renameUrl = 'save.php?action=rename';",
     "let renameUrl = '/api/save?action=rename';"),
    ("let deleteUrl = 'save.php?action=delete';",
     "let deleteUrl = '/api/save?action=delete';"),
    ("let saveReusableUrl = 'save.php?action=saveReusable';",
     "let saveReusableUrl = '/api/save?action=saveReusable';"),
    ("let oEmbedProxyUrl = 'save.php?action=oembedProxy';",
     "let oEmbedProxyUrl = '/api/save?action=oembedProxy';"),
]
for old, new in JS_VARS:
    content = content.replace(old, new)

# ── Make editor load requested page from query parameter ──
old_init = 'let firstPage = Object.keys(pages)[0];\nVvveb.Builder.init(pages[firstPage]["url"], function () {'
new_init = """let firstPage = Object.keys(pages)[0];
const urlParams = new URLSearchParams(window.location.search);
const requestedPage = urlParams.get('page');
if (requestedPage) {
\tlet pageKey = requestedPage.replace(".html", "");
\tif (pages[pageKey]) {
\t\tfirstPage = pageKey;
\t}
}
Vvveb.Builder.init(pages[firstPage]["url"], function () {"""
content = content.replace(old_init, new_init)

# ── 4. Wrap everything in {% raw %} ... {% endraw %} to avoid Jinja2 parsing conflicts ──
# We do this first so Jinja2 ignores VvvebJS's frontend micro-templates ({%= %}, {% %}).
# Then we selectively exit the {% raw %} block using {% endraw %} ... {% raw %} for our dynamic values.
content = "{% raw %}" + content + "{% endraw %}"

# ── 5. Replace defaultPages block with Escaped Jinja2 Injection ──
# Since we are inside a {% raw %} block, we use {% endraw %}{{ pages_json | safe }}{% raw %}
start = content.find("let defaultPages = {")
end = content.find("};\n\n\nlet pages = defaultPages;")
if start != -1 and end != -1:
    end += len("};\n\n\nlet pages = defaultPages;")
    content = (
        content[:start]
        + "let defaultPages = {% endraw %}{{ pages_json | safe }}{% raw %};\nlet pages = defaultPages;"
        + content[end:]
    )

# ── 6. Add back button + slug variable before </body> ───────────
# We break out of {% raw %} using {% endraw %} to interpolate {{ slug | safe }}
BACK_BTN = """
<style>
#back-to-dashboard {
  position: fixed;
  bottom: 1rem;
  left: 1rem;
  z-index: 9999;
  background: rgba(30,32,48,0.92);
  color: #a5b4fc;
  border: 1px solid rgba(99,102,241,0.4);
  border-radius: 8px;
  padding: 0.45rem 0.9rem;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  backdrop-filter: blur(8px);
  transition: background 0.2s, color 0.2s;
  text-decoration: none;
}
#back-to-dashboard:hover {
  background: rgba(99,102,241,0.25);
  color: #fff;
}
</style>
<a id="back-to-dashboard" href="/dashboard">
  &#8592; 返回管理器
</a>
<script>
// Inject project slug for save bridge
window.VVVEB_PROJECT_SLUG = "{% endraw %}{{ slug | safe }}{% raw %}";
</script>
<script src="/static/js/my-editor.js?v={% endraw %}{{ range(1, 100000) | random }}{% raw %}"></script>
"""
content = content.replace("</body>", BACK_BTN + "\n</body>")

OUT.write_text(content, encoding="utf-8")
print(f"Successfully generated escaped Jinja2 template! Size: {len(content)} bytes.")
