#!/usr/bin/env python3
"""Candidate-bound cleanup v6 preflight. Execution is intentionally unavailable now."""

from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path

ROOT = Path(__file__).resolve().parent
PROJECT = ROOT.parents[3]
FULL = "b6d78bc1e68ead7340d92b534fe3a6d0257c8aa2b02cd3d58e43215fbd8a3443"
SEALS = {
    "v4": (
        "task26-post-lifecycle-cleanup-v4-4a6c7ee5-st_01a00e68",
        "188e9e630b53e9329ae185fc8c007aecb7988c16d174d325d68e8e004edb38e9",
    ),
    "v5": (
        "task26-post-lifecycle-cleanup-v5-4a6c7ee5-st_01a00ed7",
        "0d5477d8cd143da0b254329341fdee7a90ae998d89b9f265b7c01ed48d77ba7f",
    ),
    "retention": (
        "task26-strict-rerun-retention-st_01a00ed5",
        "f02a9671422b2d49aace2037dfbcfff461249ac79d4999d46bc09ddce638367d",
    ),
    "capture": (
        "task26-strict-rerun-customer-surface-capture-st_01a00ed6",
        "fd973db6926fe12a87913814e9a15e1f396574e47d153e7221dd2efa72184484",
    ),
}


def sha(p: Path) -> str:
    return hashlib.sha256(p.read_bytes()).hexdigest()


def main() -> int:
    p = argparse.ArgumentParser()
    p.add_argument("mode", choices=("dry-run", "execute"))
    p.add_argument("--membership-pre-seal")
    p.add_argument("--membership-final-seal")
    p.add_argument("--lifecycle-seal")
    p.add_argument("--capture-receipt")
    p.add_argument("--retention-publication")
    a = p.parse_args()
    for name, (directory, expected) in SEALS.items():
        if sha(PROJECT / ".omo/evidence/task26" / directory / "SEAL.json") != expected:
            raise SystemExit(f"BLOCKED {name} seal drift")
    v4 = (
        PROJECT / ".omo/evidence/task26" / SEALS["v4"][0] / "cleanup_controller.py"
    ).read_text()
    v5 = (
        PROJECT / ".omo/evidence/task26" / SEALS["v5"][0] / "cleanup_controller.py"
    ).read_text()
    if "def write_all" not in v4 or '"passes": 2' not in v5:
        raise SystemExit("BLOCKED predecessor safety contract missing")
    required = {
        "membership_pre_seal": a.membership_pre_seal or "<FRESH_REQUIRED>",
        "membership_final_seal": a.membership_final_seal or "<FRESH_REQUIRED>",
        "lifecycle_seal": a.lifecycle_seal or "<V7_FINAL_REQUIRED>",
        "capture_receipt": a.capture_receipt or "<FINAL_IMAGE_REQUIRED>",
        "retention_publication": a.retention_publication
        or "<IMMUTABLE_PUBLICATION_REQUIRED>",
    }
    receipt = {
        "schema": "task26-strict-cleanup-controller-v6-successor-v3",
        "status": "PASS_DRY_RUN_EXECUTION_LOCKED" if a.mode == "dry-run" else "BLOCKED",
        "candidate": FULL,
        "bindings": required,
        "preserves": {
            "complete_write_atomic": True,
            "process_detection_passes": 2,
            "archive_before_mutation": True,
            "immutable_retention_required": True,
        },
        "execution_permission": False,
    }
    print(json.dumps(receipt, sort_keys=True))
    return 0 if a.mode == "dry-run" else 2


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