import re
import sys
from pathlib import Path
from bs4 import BeautifulSoup

if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8")


PROJECT_ROOT = Path(r"c:\Projects\FreeExile")
TEMPLATES_DIR = PROJECT_ROOT / "client" / "webapp" / "js" / "ui" / "templates"
INDEX_HTML = PROJECT_ROOT / "client" / "webapp" / "index.html"

def extract_templates(content):
    matches = re.finditer(r"reg\s*\(\s*['\"]([^'\"]+)['\"]\s*,\s*([`'\"])(.*?)\2\s*\)", content, re.DOTALL)
    for m in matches:
        yield m.group(1), m.group(3)

print("=== 1. CHECKING INDEX.HTML ===")
index_text = INDEX_HTML.read_text(encoding="utf-8")
soup = BeautifulSoup(index_text, "html.parser")
for el in soup.find_all(True):
    for attr in ["data-tooltip-title", "data-tooltip-desc", "title", "aria-label", "placeholder"]:
        val = el.get(attr)
        if val and ("(" in val or ")" in val):
            print(f"INDEX ATTR: <{el.name} id='{el.get('id')}'> {attr}='{val}'")
    if el.name in ["h1", "h2", "h3", "h4", "h5", "h6", "label", "button", "a"]:
        txt = el.get_text(strip=True)
        if "(" in txt or ")" in txt:
            print(f"INDEX TEXT: <{el.name} id='{el.get('id')}'> text='{txt}'")

print("\n=== 2. CHECKING TEMPLATES ===")
for tf in sorted(TEMPLATES_DIR.glob("*.js")):
    content = tf.read_text(encoding="utf-8")
    for tid, html in extract_templates(content):
        tsoup = BeautifulSoup(html, "html.parser")
        for el in tsoup.find_all(True):
            for attr in ["data-tooltip-title", "data-tooltip-desc", "title", "aria-label", "placeholder"]:
                val = el.get(attr)
                if val and ("(" in val or ")" in val):
                    print(f"TPL ATTR: {tf.name} [{tid}] <{el.name} id='{el.get('id')}'> {attr}='{val}'")
            if el.name in ["h1", "h2", "h3", "h4", "h5", "h6", "label", "button", "a"]:
                txt = el.get_text(strip=True)
                if "(" in txt or ")" in txt:
                    print(f"TPL TEXT: {tf.name} [{tid}] <{el.name} id='{el.get('id')}'> text='{txt}'")

print("\n=== 3. CHECKING ALL OTHER TAGS IN TEMPLATES (p, span, div, option) ===")
for tf in sorted(TEMPLATES_DIR.glob("*.js")):
    content = tf.read_text(encoding="utf-8")
    for tid, html in extract_templates(content):
        tsoup = BeautifulSoup(html, "html.parser")
        for el in tsoup.find_all(["p", "span", "div", "option", "li", "td", "th"]):
            # only direct strings or leaf elements to avoid repeating
            if not el.find(["p", "span", "div", "option", "li", "td", "th"]):
                txt = el.get_text(strip=True)
                if "(" in txt or ")" in txt:
                    print(f"OTHER TEXT: {tf.name} [{tid}] <{el.name}>: '{txt}'")
