# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-scoped-retention-ttl-expiry-as-a-distinct-event-from-a-right-to-be-forgotten-erasure-and-how-does-confl.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-scoped-retention-ttl-expiry-as-a-distinct-event-from-a-right-to-be-forgotten-erasure-and-how-does-confl.warrant.json
# Markdown SHA-256:   0e1f28ae9576bd15fd1e35c71db2faff38363108066bc367b1dfc366d00600c2
# Code AST SHA-256:   d3b9f3a53931369b3e289fab3b1b8cee5a2f9b8d08ec9d509fae5cf40eec67e8
# Verified Classes:   ErasureProofError, DeleteCause, RecordAttribution
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Optional


class ErasureProofError(RuntimeError):
    """Raised when an erasure proof cannot be causally attributed or closed."""


class DeleteCause(Enum):
    REQUEST_DRIVEN = "request_driven"
    PRE_EXPIRED = "pre_expired"
    CONCURRENT_AMBIGUOUS = "concurrent_ambiguous"


@dataclass
class RecordAttribution:
    record_id: str
    ttl_expired_at: Optional[float]
    request_at: float
    intent_recorded_at: Optional[float]
    is_derivative: bool
    closure_hash_present: bool


def attribute_delete(rec: RecordAttribution) -> DeleteCause:
    if rec.intent_recorded_at is None:
        raise ErasureProofError(
            f"record {rec.record_id}: no erasure-intent record; "
            "cannot attribute deletion to request (fail closed)"
        )
    if rec.is_derivative and not rec.closure_hash_present:
        raise ErasureProofError(
            f"record {rec.record_id}: derivative without closure hash; "
            "completeness claim would be vacuous"
        )
    if rec.ttl_expired_at is None:
        return DeleteCause.REQUEST_DRIVEN
    if rec.ttl_expired_at < rec.request_at:
        return DeleteCause.PRE_EXPIRED
    if rec.ttl_expired_at >= rec.request_at:
        # expired at or after request: could be forced or concurrent
        if rec.intent_recorded_at <= rec.ttl_expired_at:
            return DeleteCause.REQUEST_DRIVEN
        return DeleteCause.CONCURRENT_AMBIGUOUS


def build_erasure_proof(
    subject: str, records: list[RecordAttribution]
) -> dict:
    causes = {}
    for rec in records:
        cause = attribute_delete(rec)
        if cause is DeleteCause.CONCURRENT_AMBIGUOUS:
            raise ErasureProofError(
                f"record {rec.record_id}: TTL expiry concurrent with request; "
                "cannot distinguish forced delete from scheduled collection"
            )
        causes[rec.record_id] = cause.value
    if not any(c == DeleteCause.REQUEST_DRIVEN.value for c in causes.values()):
        raise ErasureProofError(
            f"subject {subject}: no request-driven deletions; "
            "proof would be absence-only and semantically false"
        )
    return {
        "subject": subject,
        "causes": causes,
        "closure_complete": all(r.closure_hash_present for r in records),
    }
