"""Guard the balanced Login layout and company registration flow."""
from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path

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


def main() -> int:
    app = (ROOT / "app.py").read_text(encoding="utf-8", errors="ignore")
    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 ""
    glow_assign = app.find("glow_fill = _mix")
    glow_use = app.find("fill=_mix(glow_fill")
    checks = {
        "login_glow_fill_defined_before_use": glow_assign != -1 and glow_use != -1 and glow_assign < glow_use,
        "wide_login_refined_shell": "shell_w = min(cw - shell_margin_x * 2, max(1360, cw * 0.785))" in app and "shell_h = min(ch - shell_margin_y * 2, max(800, ch * 0.895))" in app,
        "wide_login_not_tiny_on_desktop": "shell_w = min(cw - shell_margin_x * 2, max(1360, cw * 0.785))" in app,
        "login_background_ui_refined": "motion_enabled = bool(self.user_settings.get(\"login_background_motion\", True))" in app and "pulse_login_background" in app and "milestone track, upgraded kinetic glass aurora style" in app,
        "wide_login_no_near_fullscreen_shell": "shell_w = max(1320, cw - shell_margin_x * 2)" not in app and "cw * 0.785" in app,
        "wide_login_centered_field": "field_w = min(right_w - 120*s, max(548*s, right_w * 0.84))" in app and "form_x = x0 + left_w + 54*s" in app and "rx = form_x" in app,
        "wide_login_recent_accounts_adaptive": "add_account_chip" in app and "place_recent_chips(rx, chip_y, recent_chip_w, 38*s, 14*s)" in app,
        "compact_login_smaller_safe_shell": "target_w = cw * (1.00 if tiny_login else 0.96)" in app and "target_h = ch * (0.95 if tiny_login else 0.91)" in app,
        "compact_login_centered_form": "content_max_w = min(540*s" in app and "content_x = x0 + (shell_w - content_max_w) / 2" in app,
        "login_actions_balanced": "footer_y = btn_y + btn_h + 96*s" in app and "footer_h = 142*s" in app and "Copyright © NexaOffice 2026" in app,
        "login_utility_bar_clean": "util_theme_w = 198*s" in app and "set_win(\"login_lang_menu\", util_lang_x+6*s" in app and "theme_menu_btn.configure(bg=login_panel_right" in app,
        "responsive_login_profiles": "login_profile = \"wide\"" in app and "min(1.26, base_scale)" in app and "min(1.20, s)" in app,
        "login_resize_debounce": "def schedule_draw_login" in app and "canvas.bind(\"<Configure>\", schedule_draw_login)" in app,
        "login_static_first_motion_guard": "motion_enabled = bool(self.user_settings.get(\"login_background_motion\", True))" in app and "anim_state = {" in app and "bool(self.user_settings.get(\"login_background_motion\", True))" in app,
        "login_adaptive_animation_cadence": "login_background_motion_intensity" in app and "after_ms = min(1280" in app and "after_ms = max(430" in app,
        "company_registration_opens_directly": "Enterprise account creation is allowed directly" in reg and "register_personal_required" not in reg,
        "company_registration_dialog_hidden_until_centered": "dlg.withdraw()" in reg and "dlg.deiconify()" in reg and "center_window(dlg, self.root, 620" in reg,
        "forgot_dialog_hidden_until_centered": "def do_forgot_password" in app and "_show_forgot_dialog" in app and "dlg.withdraw()" in app and "center_window(dlg, self.root, 600" in app,
        "login_dropdown_motion_pause": "def _pause_login_motion" in app and "menu_open_until" in app and "<Button-1>" in app,
        "company_duplicate_checks_remain": "register_duplicate_company" in reg and "register_duplicate_email" in reg and "register_duplicate_phone" in reg,
    }
    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("Login layout guard", payload["status"])
    for issue in issues:
        print("-", issue)
    return 0 if not issues else 1


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