"""Guard offline-first network deployment planning added in v2026.06.20.45."""
from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path

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


def main() -> int:
    errors: list[str] = []
    network_py = (ROOT / "core" / "network.py").read_text(encoding="utf-8", errors="ignore")
    sync_py = (ROOT / "core" / "sync_queue.py").read_text(encoding="utf-8", errors="ignore")
    app_py = (ROOT / "app.py").read_text(encoding="utf-8", errors="ignore")
    for marker in [
        "def network_deployment_plan",
        '"will_connect_now": False',
        '"token_displayed": False',
        '"ready_for_system_cutover"',
        '"dry_run"',
        '"connect_only_after_system_config"',
        'def server_cutover_contract',
        '"server_contract"',
        '"scope_isolation_required": True',
        '"POST /api/bootstrap"',
        '"POST /api/sync/batch"',
    ]:
        if marker not in network_py:
            errors.append(f"core/network.py missing marker: {marker}")
    for marker in [
        "def backup_sync_queue",
        "SYNC_QUEUE_BACKUP_DIR",
        "def sync_queue_integrity_report",
        "def sync_cutover_dry_run",
        '"will_connect": False',
        '"duplicated_pending"',
        '"secret_hits"',
        "cleaned_reversed",
        "seen_pending",
    ]:
        if marker not in sync_py:
            errors.append(f"core/sync_queue.py missing marker: {marker}")
    for marker in [
        "get_network_deployment_plan",
        "get_sync_cutover_dry_run",
        "ready_for_system_cutover",
        "network_deployment_plan",
    ]:
        if marker not in app_py:
            errors.append(f"app.py missing marker: {marker}")
    report = {
        "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(report, ensure_ascii=False, indent=2), encoding="utf-8")
    print("Network deployment plan guard", report["status"])
    for err in errors:
        print("-", err)
    return 0 if not errors else 1


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