# -*- coding: utf-8 -*-
import re, pathlib

ROOT = pathlib.Path(r"C:\Users\Семья\alrent")

def analyze(path: pathlib.Path):
    raw = path.read_bytes()
    text = raw.decode("utf-8", errors="replace")
    issues = []
    # mojibake markers
    if "\ufffd" in text:
        issues.append(f"replacement-char x{text.count(chr(0xFFFD))}")
    # UTF-8 cyrillic mis-decoded as latin1: sequences starting with C3 90 / C3 91 etc displayed wrong
    if re.search(r"(?:Ð[\x80-\xbf]|Ñ[\x80-\xbf]){3,}", text):
        issues.append("utf8-as-latin1")
    # cp1251 mojibake like Р№, С‚
    if re.search(r"[РС][\u0400-\u04FF]{1,}", text):
        issues.append("cp1251-mojibake")
    # raw hash keys
    keys = re.findall(r"tr_[a-f0-9]{32}", text)
    # placeholders not filled
    braces = re.findall(r"\{[a-zA-Z_][a-zA-Z0-9_]*\}", text)
    # weird: cyrillic mixed with high symbols
    weird_chars = sorted(set(ch for ch in text if ord(ch) > 127 and not (
        0x0400 <= ord(ch) <= 0x04FF or 0x00A0 <= ord(ch) <= 0x00FF
        or 0x2010 <= ord(ch) <= 0x203F or 0x2000 <= ord(ch) <= 0x200F
        or 0x20BD == ord(ch) or 0x2190 <= ord(ch) <= 0x21FF
        or 0x2600 <= ord(ch) <= 0x27BF or 0x1F300 <= ord(ch) <= 0x1FAFF
        or 0x2200 <= ord(ch) <= 0x22FF or 0x00D7 == ord(ch)
    )))
    return {
        "size": len(raw),
        "issues": issues,
        "keys": keys[:10],
        "key_count": len(keys),
        "braces": braces[:15],
        "weird": weird_chars[:20],
        "text": text,
    }

print("===== MAIL TEMPLATES =====")
for p in sorted((ROOT / "resources" / "mail").glob("*.tpl")):
    r = analyze(p)
    flag = "!!" if r["issues"] or r["weird"] else "  "
    print(f"{flag} {p.name:40} size={r['size']:5} issues={r['issues']} keys={r['key_count']} braces={r['braces'][:6]} weird={r['weird']}")

print("\n===== PROFILE TEMPLATES =====")
paths = list((ROOT / "resources" / "view" / "web" / "profile").glob("*.tpl"))
paths += [ROOT / "resources" / "view" / "web" / "profile.tpl"]
paths += list((ROOT / "resources" / "view" / "web" / "components").glob("*profile*"))
paths += list((ROOT / "resources" / "view" / "web" / "components" / "items").glob("profile*"))
paths += list((ROOT / "resources" / "view" / "web" / "components" / "modals").glob("profile*"))
for p in sorted(set(paths)):
    if p.is_dir():
        continue
    r = analyze(p)
    flag = "!!" if r["issues"] or r["weird"] else "  "
    print(f"{flag} {p.name:40} size={r['size']:5} issues={r['issues']} keys={r['key_count']} weird={r['weird']}")

print("\n===== PROFILE JS =====")
for p in [ROOT / "resources" / "view" / "web" / "assets" / "js" / "profile.js"]:
    r = analyze(p)
    print(f"   {p.name} issues={r['issues']} keys={r['key_count']} weird={r['weird']}")
    # show any non-ascii strings in JS
    strings = re.findall(r"['\"]([^'\"]{3,120})['\"]", r["text"])
    non_ascii = [s for s in strings if any(ord(c) > 127 for c in s)]
    print(f"   non-ascii strings: {len(non_ascii)}")
    for s in non_ascii[:30]:
        print(f"     {s!r}")
