"""Guard local data integrity checkpoint features added in v2026.06.20.47."""
from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
REPORT = ROOT / "data" / "data_integrity_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")
    for marker in [
        "def data_integrity_report",
        "def create_preflight_data_checkpoint",
        "def ensure_preflight_data_checkpoint",
        "preflight_checkpoint_last_action.json",
        "def list_preflight_checkpoints",
        "def checkpoint_health_report",
        "def checkpoint_restore_dry_run",
        "CHECKPOINT_LIMIT",
        "settings.redacted.json",
        '"secret_values_displayed": False',
        '"will_connect": False',
        "CRITICAL_JSON_FILES",
    ]:
        if marker not in data_py:
            errors.append(f"core/data_integrity.py missing marker: {marker}")
    for marker in [
        "get_data_integrity_report",
        "create_network_preflight_checkpoint",
        "force=False",
        "force=True",
        "get_checkpoint_health_report",
        "get_checkpoint_restore_dry_run",
        "network_bootstrap",
        "prepare_sync_snapshot",
    ]:
        if marker not in app_py:
            errors.append(f"app.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("Data integrity guard", payload["status"])
    for err in errors:
        print("-", err)
    return 0 if not errors else 1


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