"""Guard offline maintenance summary added in v2026.06.20.50."""
from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path

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


def main() -> int:
    errors: list[str] = []
    maintenance_py = (ROOT / "core" / "preflight_maintenance.py").read_text(encoding="utf-8", errors="ignore")
    app_py = (ROOT / "app.py").read_text(encoding="utf-8", errors="ignore")
    settings_py = (ROOT / "pages" / "settings_page.py").read_text(encoding="utf-8", errors="ignore")
    for marker in [
        "def build_offline_maintenance_report",
        "offline_maintenance_latest.json",
        '"will_connect": False',
        '"secret_values_displayed": False',
        '"deployment_dry_run_only"',
        '"cutover_dry_run_only"',
        '"tokens_hidden"',
    ]:
        if marker not in maintenance_py:
            errors.append(f"core/preflight_maintenance.py missing marker: {marker}")
    for marker in [
        "get_offline_maintenance_report",
        "build_offline_maintenance_report",
        "data_integrity=data_report",
        "cutover_dry_run=cutover",
    ]:
        if marker not in app_py:
            errors.append(f"app.py missing marker: {marker}")
    for marker in [
        "open_offline_maintenance_report",
        "server_sync_open_maintenance",
        "server_sync_maintenance_status",
    ]:
        if marker not in settings_py:
            errors.append(f"pages/settings_page.py missing marker: {marker}")
    payload = {
        "time": datetime.now().isoformat(timespec="seconds"),
        "status": "OK" if not errors else "FAILED",
        "issues": errors,
    }
    REPORT.parent.mkdir(parents=True, exist_ok=True)
    REPORT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    print("Offline maintenance guard", payload["status"])
    for err in errors:
        print("-", err)
    return 0 if not errors else 1


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