#!/usr/bin/env python3
"""Create the deterministic v6 artifact inventory and seal exactly once."""

from __future__ import annotations

import hashlib
import json
import os
from pathlib import Path

ROOT = Path(__file__).resolve().parent
SEALED = (
    "OPERATIONS.md",
    "continuous_lifecycle.py",
    "independent_verify.py",
    "observer_v63.py",
    "readiness.json",
    "seal_artifacts.py",
    "test_continuous_lifecycle.py",
    "test_observer_v63.py",
)


def digest(path: Path) -> str:
    return hashlib.sha256(path.read_bytes()).hexdigest()


def exclusive(path: Path, value: object) -> None:
    fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_CLOEXEC, 0o600)
    try:
        os.write(fd, (json.dumps(value, sort_keys=True, separators=(",", ":")) + "\n").encode())
        os.fsync(fd)
    finally:
        os.close(fd)


def main() -> int:
    files = [{"path": name, "sha256": digest(ROOT / name)} for name in SEALED]
    inventory = {
        "schema": "task26-observer-v6.3-hash-inventory-v1",
        "files": files,
        "sealed_file_count": len(files),
    }
    exclusive(ROOT / "hash-inventory.json", inventory)
    seal = {
        "schema": "task26-continuous-lifecycle-observer-v6.3-seal-v1",
        "status": "PASS_SEALED_SIX_HOUR_CUSTOMER_WAIT",
        "predecessor_root": "task26-continuous-lifecycle-observer-v6.2-f38d0373",
        "predecessor_seal_sha256": "ca54d55c79edcd6e87eae90646191bab55943980dce21f3e8b647b9dd900fc69",
        "candidate": "f38d0373a58877806ff64a9cac54ab01e6e6e47f7101e3dcfd5ffbf05f8bb101",
        "core": "5620cf756b3f32901091475a5ed409c1757eeaf143ecb1e4a23a452d3d9e3e8e",
        "profile_wheel": "f75856d6d986b64d3d2f083aec2f865c7aea19f5950b84ff06e519f2f6505af6",
        "hermes_wheel": "2110071bc2761e6cbd149de4694dd7328148ec7e5b2e3c7c10a2709082d5e161",
        "deployment_receipt": "8cd6edb7775804302703e20153f23127dd709adbcc24fe81bffb89e5f33f7136",
        "incident_receipt": "1b2e8939e9c44297114906f8b608035102eb673a2d7eaa78f160af3cb9917ab4",
        "candidate_inventory": "95265a60e4811697858cd379d37cd4251d6b22a926009055951a025dc2ef7cc5",
        "recovery_seal": "00d44de03e8dde668da8058091751291b13457c8bd086917dfe85e42b3813df8",
        "polling_offset_lower_bound": 629525117,
        "inventory_sha256": digest(ROOT / "hash-inventory.json"),
        "sealed_file_count": len(files),
        "tests_passed": 34,
        "timeout_defaults": {"stage": 1800, "total": 21600},
        "timeout_caps": {"stage": 21600, "total": 21600},
        "deployed_current_state": "PASS_ACTIVE_ZERO_DELIVERY_RECOVERED_OFFSET",
        "profile_module_parity_count": 44,
        "lsp_errors": 0,
        "ruff": "PASS",
        "compile": "PASS",
        "network_calls": 0,
        "profile_mutated": False,
        "service_actions": 0,
        "ui_actions": 0,
    }
    exclusive(ROOT / "SEAL.json", seal)
    print(json.dumps(seal, sort_keys=True))
    return 0


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