"""Guard registration and sticker UX rules added in v2026.06.20.39."""
from __future__ import annotations

import json
import re
from datetime import datetime
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
REPORT = ROOT / "data" / "registration_sticker_guard_latest.json"


def main() -> int:
    app = (ROOT / "app.py").read_text(encoding="utf-8", errors="ignore")
    chat = (ROOT / "pages" / "chat_page.py").read_text(encoding="utf-8", errors="ignore")
    manifest = json.loads((ROOT / "assets" / "chat_stickers" / "manifest.json").read_text(encoding="utf-8"))
    reg_start = app.find("def _registration_dialog")
    reg_end = app.find("def do_create_personal", reg_start)
    reg = app[reg_start:reg_end] if reg_start != -1 and reg_end != -1 else ""
    checks = {
        "registration_no_tutup_button": 'self.t("profile_close")' not in reg,
        "captcha_refresh": "register_refresh_code" in reg and "refresh_code" in reg and "captcha_state" in reg,
        "email_validation": "email_valid" in reg and "register_email_invalid" in reg,
        "phone_validation": "register_phone_invalid" in reg and r"\D" in reg,
        "password_rule_and_eye": "password_valid" in reg and "register_password_weak" in reg and "👁" in reg,
        "role_only_in_company_registration": "role_create_var" in reg and "account_role" in reg,
        "chat_personal_first": 'self._scope_button(self.scope_row, "chat_scope_personal_short", "personal")' in chat,
        "sticker_image_only_messages": "Sticker messages are image-only" in chat and 'self.app.t("sticker_preview")' in chat,
        "manifest_no_cjk_labels": not any(re.search(r"[\u4e00-\u9fff]", str(item.get("label", ""))) for pack in manifest.get("packs", []) for item in pack.get("items", [])),
    }
    issues = [name for name, ok in checks.items() if not ok]
    payload = {
        "time": datetime.now().isoformat(timespec="seconds"),
        "status": "OK" if not issues else "FAILED",
        "checks": checks,
        "issues": issues,
    }
    REPORT.parent.mkdir(parents=True, exist_ok=True)
    REPORT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    print("Registration/sticker guard", payload["status"])
    for issue in issues:
        print("-", issue)
    return 0 if not issues else 1


if __name__ == "__main__":
    raise SystemExit(main())
