import re
from pathlib import Path

TEMPLATES_DIR = Path('client/webapp/templates')
JS_DIR = Path('client/webapp/js/ui/templates')

# Read catalogs
catalog_htmls = {}
reg_pattern = re.compile(r'reg\(\s*[\'"]([^\'"]+)[\'"]\s*,\s*`([\s\S]*?)`\s*\);')

for cat_file in sorted(JS_DIR.glob('*.js')):
    content = cat_file.read_text(encoding='utf-8')
    for match in reg_pattern.finditer(content):
        modal_id = match.group(1)
        html = match.group(2).strip()
        catalog_htmls[modal_id] = (cat_file.name, html)

print(f'Total catalog modals found: {len(catalog_htmls)}')

# Check each template file
mismatches = 0
for tpl_file in sorted(TEMPLATES_DIR.glob('*.html')):
    tpl_content = tpl_file.read_text(encoding='utf-8').strip()
    id_match = re.search(r'id=[\'"]([^\'"]+)[\'"]', tpl_content)
    if not id_match:
        print(f'No ID found in {tpl_file.name}')
        continue
    modal_id = id_match.group(1)
    if modal_id not in catalog_htmls:
        print(f'Modal {modal_id} from {tpl_file.name} NOT in catalogs!')
        mismatches += 1
    else:
        cat_file, cat_content = catalog_htmls[modal_id]
        if tpl_content != cat_content:
            print(f'Content MISMATCH for {modal_id} between {tpl_file.name} and {cat_file}!')
            mismatches += 1

print(f'Total mismatches: {mismatches}')
