"""Guard Personal / Enterprise account isolation.

Personal accounts must not unlock enterprise modules just because another
enterprise account exists on the same PC. Personal mode may use the Personal
Chat module and Personal friends, but Enterprise, Cloud, company staff, and
company groups stay hidden. The only enterprise entry points in Personal mode
are Join Enterprise and Create Enterprise.
"""
from __future__ import annotations

import json
from pathlib import Path
from datetime import datetime

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


def main() -> int:
    app = (ROOT / "app.py").read_text(encoding="utf-8", errors="ignore")
    home = (ROOT / "pages" / "home_page.py").read_text(encoding="utf-8", errors="ignore")
    errors: list[str] = []
    markers = {
        "current_account_lookup": "def current_account(self):" in app and "Other accounts saved on the same PC must not unlock enterprise UI" in app,
        "current_account_enterprise_gate": "def current_account_has_enterprise_access" in app and "return False\n            return bool(str(self.user_settings.get" in app,
        "has_enterprise_current_only": "def has_enterprise_account(self):\n        return self.current_account_has_enterprise_access()" in app,
        "strict_personal_allowed_pages": "def personal_allowed_pages(self):" in app and "return {\"home\", \"calendar\", \"files\", \"chat\", \"ai\", \"settings\"}" in app,
        "enterprise_only_pages_declared": "def enterprise_only_pages(self):" in app and "return {\"enterprise\", \"cloud\"}" in app,
        "personal_page_gate": "def is_page_available(self, page):" in app and "return page in self.personal_allowed_pages()" in app,
        "personal_mode_boundary_enforcer": "def _enforce_account_mode_boundaries" in app and "actions[key] = False" in app and "notifications[\"show_enterprise\"] = False" in app,
        "personal_sidebar_only_two_enterprise_actions": "personal_enterprise_gate" in app and "join_enterprise_btn" in app and "create_enterprise_btn" in app,
        "quick_launcher_personal_two_actions": "join_enterprise" in app and "create_enterprise" in app and "commands = [cmd for cmd in commands if cmd.get(\"id\") not in {\"cloud\", \"enterprise\"}]" in app,
        "personal_quick_actions_hide_company": "for key in (\"cloud\", \"enterprise\"):" in app and "actions[key] = False" in app and "actions[\"chat\"] = True" in app,
        "home_personal_actions_join_create": "self.app.show_join_enterprise_dialog" in home and "self.app.show_create_enterprise_dialog" in home,
        "home_personal_hides_company_launchers": "\"home_app_cloud\"" in home and "\"home_app_approval\"" in home and "\"home_app_meeting\"" in home,
        "enterprise_activation_explicit": "def _activate_enterprise_for_current_account" in app and "account[\"account_type\"] = \"enterprise\"" in app,
    }
    for name, ok in markers.items():
        if not ok:
            errors.append(name)
    for lang in ("zh_CN", "en_US", "id_ID"):
        data = json.loads((ROOT / "lang" / f"{lang}.json").read_text(encoding="utf-8"))
        for key in ("personal_enterprise_gate_title", "join_enterprise_btn", "create_enterprise_btn", "enterprise_joined_success", "enterprise_created_success"):
            if key not in data:
                errors.append(f"{lang} missing {key}")
    payload = {"time": datetime.now().isoformat(timespec="seconds"), "status": "OK" if not errors else "FAILED", "checks": markers, "errors": errors}
    REPORT.parent.mkdir(parents=True, exist_ok=True)
    REPORT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    print("Personal/Enterprise isolation guard", payload["status"])
    for err in errors:
        print("-", err)
    return 0 if not errors else 1


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