"""Runtime check for v2026.06.20.46 sync queue repair semantics."""
from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path

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


def main() -> int:
    errors: list[str] = []
    src = (ROOT / "core" / "sync_queue.py").read_text(encoding="utf-8", errors="ignore")
    for marker in [
        "def backup_sync_queue",
        "SYNC_QUEUE_BACKUP_LIMIT",
        "cleaned_reversed",
        "seen_pending",
        "def _has_unredacted_secret",
        "make_backup=False",
    ]:
        if marker not in src:
            errors.append(f"core/sync_queue.py missing marker: {marker}")
    app = (ROOT / "app.py").read_text(encoding="utf-8", errors="ignore")
    for marker in [
        "backup_sync_queue(self.sync_queue, reason=\"manual_repair\")",
        "backup_sync_queue(self.sync_queue, reason=\"prepare_snapshot\")",
        "repair_sync_queue(self.sync_queue, make_backup=False)",
    ]:
        if marker not in app:
            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("Sync queue repair guard", payload["status"])
    for err in errors:
        print("-", err)
    return 0 if not errors else 1


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