#!/usr/bin/env python3
"""
rulebook_review.py — Generate an HTML review page for the High Orbit HU rulebook translation.

Reads translations/rulebook.md and produces output/rulebook_review.html — a clean,
section-by-section view of the Hungarian text for side-by-side review against the
original English PDF.

Usage:
    python scripts/rulebook_review.py

Open the generated HTML in a browser, and open the original PDF alongside it:
    source/High_Orbit/High Orbit/High_Orbit_Rules_(2019-05-08_v2).pdf
"""

import os
import re
import html

# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
BASE_DIR        = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
RULEBOOK_MD     = os.path.join(BASE_DIR, "translations", "rulebook.md")
OUTPUT_DIR      = os.path.join(BASE_DIR, "output")
OUTPUT_HTML     = os.path.join(OUTPUT_DIR, "rulebook_review.html")
ORIGINAL_PDF    = os.path.join(BASE_DIR, r"source\High_Orbit\High Orbit\High_Orbit_Rules_(2019-05-08_v2).pdf")


# ---------------------------------------------------------------------------
# Markdown → HTML conversion (minimal, sufficient for this document)
# ---------------------------------------------------------------------------

def md_inline(text: str) -> str:
    """Convert inline markdown (bold, italic, code) to HTML."""
    # Bold+italic
    text = re.sub(r"\*\*\*(.+?)\*\*\*", r"<strong><em>\1</em></strong>", text)
    # Bold
    text = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", text)
    # Italic
    text = re.sub(r"\*(.+?)\*", r"<em>\1</em>", text)
    # Code
    text = re.sub(r"`(.+?)`", r"<code>\1</code>", text)
    return text


def parse_md(md_text: str) -> list:
    """
    Parse the markdown into a list of block dicts:
        {"type": "h1"|"h2"|"h3"|"hr"|"blockquote"|"table"|"list"|"paragraph", "content": ...}
    """
    blocks = []
    lines = md_text.splitlines()
    i = 0
    list_buffer = []
    table_buffer = []

    def flush_list():
        if list_buffer:
            blocks.append({"type": "list", "items": list(list_buffer)})
            list_buffer.clear()

    def flush_table():
        if table_buffer:
            blocks.append({"type": "table", "rows": list(table_buffer)})
            table_buffer.clear()

    while i < len(lines):
        line = lines[i]

        # Headings
        m = re.match(r"^(#{1,3})\s+(.*)", line)
        if m:
            flush_list()
            flush_table()
            level = len(m.group(1))
            blocks.append({"type": f"h{level}", "content": m.group(2).strip()})
            i += 1
            continue

        # Horizontal rule
        if re.match(r"^-{3,}$", line.strip()):
            flush_list()
            flush_table()
            blocks.append({"type": "hr"})
            i += 1
            continue

        # Blockquote
        if line.startswith(">"):
            flush_list()
            flush_table()
            content = re.sub(r"^>\s?", "", line)
            blocks.append({"type": "blockquote", "content": content})
            i += 1
            continue

        # Table row
        if line.strip().startswith("|"):
            flush_list()
            # skip separator lines like |---|---|
            if re.match(r"^\|[-| :]+\|$", line.strip()):
                i += 1
                continue
            cells = [c.strip() for c in line.strip().strip("|").split("|")]
            table_buffer.append(cells)
            i += 1
            continue
        else:
            flush_table()

        # Bullet list item
        m = re.match(r"^[-*]\s+(.*)", line)
        if m:
            list_buffer.append(m.group(1))
            i += 1
            continue
        else:
            flush_list()

        # Blank line — separator
        if line.strip() == "":
            i += 1
            continue

        # Paragraph (collect consecutive non-special lines)
        para_lines = []
        while i < len(lines):
            ln = lines[i]
            if (ln.strip() == ""
                    or ln.startswith("#")
                    or ln.startswith(">")
                    or ln.strip().startswith("|")
                    or re.match(r"^[-*]\s+", ln)
                    or re.match(r"^-{3,}$", ln.strip())):
                break
            para_lines.append(ln)
            i += 1
        if para_lines:
            blocks.append({"type": "paragraph", "content": " ".join(para_lines)})
        continue

    flush_list()
    flush_table()
    return blocks


def blocks_to_html(blocks: list) -> str:
    parts = []
    section_count = 0

    for block in blocks:
        t = block["type"]

        if t == "hr":
            parts.append('<hr class="section-divider">')

        elif t == "h1":
            parts.append(f'<h1>{md_inline(html.escape(block["content"]))}</h1>')

        elif t == "h2":
            section_count += 1
            content = md_inline(html.escape(block["content"]))
            parts.append(
                f'<div class="section-header">'
                f'<span class="section-num">§{section_count}</span>'
                f'<h2>{content}</h2>'
                f'</div>'
            )

        elif t == "h3":
            content = md_inline(html.escape(block["content"]))
            parts.append(f'<h3>{content}</h3>')

        elif t == "blockquote":
            content = md_inline(html.escape(block["content"]))
            parts.append(f'<blockquote>{content}</blockquote>')

        elif t == "paragraph":
            content = md_inline(html.escape(block["content"]))
            parts.append(f'<p>{content}</p>')

        elif t == "list":
            items_html = "".join(
                f'<li>{md_inline(html.escape(item))}</li>'
                for item in block["items"]
            )
            parts.append(f'<ul>{items_html}</ul>')

        elif t == "table":
            rows = block["rows"]
            if not rows:
                continue
            header_row = rows[0]
            th_html = "".join(
                f'<th>{md_inline(html.escape(c))}</th>' for c in header_row
            )
            body_html = ""
            for row in rows[1:]:
                td_html = "".join(
                    f'<td>{md_inline(html.escape(c))}</td>' for c in row
                )
                body_html += f'<tr>{td_html}</tr>'
            parts.append(
                f'<table><thead><tr>{th_html}</tr></thead>'
                f'<tbody>{body_html}</tbody></table>'
            )

    return "\n".join(parts)


# ---------------------------------------------------------------------------
# HTML template
# ---------------------------------------------------------------------------

HTML_TEMPLATE = """\
<!DOCTYPE html>
<html lang="hu">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>High Orbit: Magas Pálya — Szabálykönyv review</title>
  <style>
    *, *::before, *::after {{ box-sizing: border-box; }}

    body {{
      font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
      font-size: 16px;
      line-height: 1.65;
      background: #f4f6f8;
      color: #1a1a2e;
      margin: 0;
      padding: 0;
    }}

    /* ── Sticky banner ── */
    .banner {{
      position: sticky;
      top: 0;
      z-index: 100;
      background: #1a1a2e;
      color: #e0e6f0;
      padding: 10px 32px;
      font-size: 13px;
      display: flex;
      align-items: center;
      gap: 16px;
      box-shadow: 0 2px 6px rgba(0,0,0,.35);
    }}
    .banner strong {{ color: #ffd166; }}
    .banner a {{ color: #90caf9; text-decoration: none; }}
    .banner a:hover {{ text-decoration: underline; }}

    /* ── Main container ── */
    .container {{
      max-width: 860px;
      margin: 0 auto;
      padding: 40px 32px 80px;
    }}

    h1 {{
      font-size: 2rem;
      margin-bottom: 4px;
      color: #1a1a2e;
    }}
    h1 + p {{ color: #555; margin-top: 0; }}

    /* ── Section headers ── */
    .section-header {{
      display: flex;
      align-items: baseline;
      gap: 12px;
      margin-top: 2.5rem;
      margin-bottom: 0.25rem;
      padding-bottom: 6px;
      border-bottom: 2px solid #1a1a2e;
    }}
    .section-num {{
      font-size: 0.85rem;
      font-weight: 700;
      color: #fff;
      background: #1a1a2e;
      padding: 2px 8px;
      border-radius: 4px;
      letter-spacing: .05em;
      white-space: nowrap;
    }}
    .section-header h2 {{
      margin: 0;
      font-size: 1.3rem;
      color: #1a1a2e;
    }}

    h3 {{
      font-size: 1.05rem;
      margin-top: 1.4rem;
      margin-bottom: 0.3rem;
      color: #2c3e70;
    }}

    p {{ margin: 0.6rem 0; }}

    ul {{
      margin: 0.5rem 0 0.5rem 1.4rem;
      padding: 0;
    }}
    li {{ margin: 0.25rem 0; }}

    blockquote {{
      border-left: 4px solid #ffd166;
      background: #fff8e1;
      margin: 1rem 0;
      padding: 10px 16px;
      border-radius: 0 4px 4px 0;
      font-style: italic;
      color: #4a3800;
    }}

    table {{
      border-collapse: collapse;
      width: 100%;
      margin: 1rem 0;
      font-size: 0.95rem;
    }}
    th {{
      background: #1a1a2e;
      color: #e0e6f0;
      padding: 8px 12px;
      text-align: left;
    }}
    td {{
      padding: 7px 12px;
      border-bottom: 1px solid #dde3ed;
    }}
    tr:nth-child(even) td {{ background: #f0f3f8; }}

    code {{
      background: #e8ecf2;
      padding: 1px 5px;
      border-radius: 3px;
      font-family: "Consolas", monospace;
      font-size: 0.88em;
    }}

    hr.section-divider {{
      border: none;
      border-top: 1px solid #ccd3e0;
      margin: 2rem 0;
    }}

    /* ── Status badge ── */
    .status-badge {{
      display: inline-block;
      background: #ffd166;
      color: #1a1a2e;
      font-weight: 700;
      font-size: 0.8rem;
      padding: 3px 10px;
      border-radius: 12px;
      margin-left: 8px;
      vertical-align: middle;
      letter-spacing: .04em;
    }}

    /* ── Footer ── */
    .footer {{
      margin-top: 60px;
      font-size: 0.8rem;
      color: #999;
      text-align: center;
    }}
  </style>
</head>
<body>

<div class="banner">
  <span>&#x1F4D6; <strong>Fordítás review</strong> — nyisd meg a PDF-et egymás mellé az összehasonlításhoz:</span>
  <span><strong>High_Orbit_Rules_(2019-05-08_v2).pdf</strong></span>
</div>

<div class="container">

  <h1>High Orbit: Magas Pálya
    <span class="status-badge">VÁZLAT</span>
  </h1>
  <p>Magyar fordítás — oldalonkénti review oldal. Az eredeti angol PDF-et nyisd meg párhuzamosan az összehasonlításhoz.</p>
  <hr class="section-divider">

{body}

  <div class="footer">
    Generálva: {generated}<br>
    Forrás: <code>translations/rulebook.md</code> &mdash;
    Script: <code>scripts/rulebook_review.py</code>
  </div>
</div>

</body>
</html>
"""


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

def main():
    if not os.path.isfile(RULEBOOK_MD):
        print(f"ERROR: Translation file not found: {RULEBOOK_MD}")
        raise SystemExit(1)

    with open(RULEBOOK_MD, encoding="utf-8") as f:
        md_text = f.read()

    blocks = parse_md(md_text)
    body_html = blocks_to_html(blocks)

    os.makedirs(OUTPUT_DIR, exist_ok=True)

    from datetime import datetime
    generated = datetime.now().strftime("%Y-%m-%d %H:%M")

    html_out = HTML_TEMPLATE.format(body=body_html, generated=generated)

    with open(OUTPUT_HTML, "w", encoding="utf-8") as f:
        f.write(html_out)

    print(f"OK  -> {OUTPUT_HTML}")

    if os.path.isfile(ORIGINAL_PDF):
        print(f"PDF -> {ORIGINAL_PDF}")
    else:
        print(f"NOTE: Original PDF not found locally (bind-mounted at runtime):")
        print(f"      {ORIGINAL_PDF}")


if __name__ == "__main__":
    main()
