"""Guard checkpoint throttling added in v2026.06.20.49."""
from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path

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


def main() -> int:
    errors: list[str] = []
    data_py = (ROOT / "core" / "data_integrity.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 ensure_preflight_data_checkpoint",
        "min_interval_seconds",
        '"reused": True',
        "preflight_checkpoint_last_action.json",
        "latest_age_seconds",
    ]:
        if marker not in data_py:
            errors.append(f"core/data_integrity.py missing marker: {marker}")
    for marker in [
        "force=False, min_interval_seconds=6 * 60 * 60",
        'create_network_preflight_checkpoint("network_bootstrap", force=False',
        'create_network_preflight_checkpoint("prepare_sync_snapshot", force=True',
    ]:
        if marker not in app_py:
            errors.append(f"app.py missing marker: {marker}")
    if 'create_network_preflight_checkpoint("manual_settings_open", force=True)' not in settings_py:
        errors.append("Settings manual checkpoint action must force-create a checkpoint")
    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("Preflight checkpoint throttle guard", payload["status"])
    for err in errors:
        print("-", err)
    return 0 if not errors else 1


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