# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-valid-verifiable-credential-whose-issuer-did-document-has-silently-rotated-its-verification-method-mid-handsh.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-valid-verifiable-credential-whose-issuer-did-document-has-silently-rotated-its-verification-method-mid-handsh.warrant.json
# Markdown SHA-256:   f4a4e6bfd171f05b2247b60d8421b10860f884e98b8d9a67106cc87d6cf4d785
# Code AST SHA-256:   fafc9a14ad04b8156adecafc4fad94d8d256aa5c8612fdb699bbf8a596a49968
# Verified Classes:   AttestationBoundaryError, VerificationMethod, RevocationRecord
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AttestationBoundaryError(RuntimeError):
    """Raised when a VC cannot be authorized against the historical DID state."""


@dataclass
class VerificationMethod:
    id: str
    valid_from: float
    valid_until: float | None  # None = still active
    purpose: str  # e.g. "assertionMethod"


@dataclass
class RevocationRecord:
    method_id: str
    effective_time: float
    reason: str  # "rotation" | "compromise"


def authorize_credential(
    vc_proof_method: str,
    vc_proof_created: float,
    historical_methods: list[VerificationMethod],
    revocations: list[RevocationRecord],
    grace_window: float,
    versioned_resolution_available: bool,
    now: float | None = None,
) -> bool:
    now = now if now is not None else time.time()

    if not versioned_resolution_available:
        # Cannot reconstruct historical assertionMethod state: fail closed
        # unless within a bounded grace window.
        if now - vc_proof_created <= grace_window:
            return True
        raise AttestationBoundaryError(
            f"No versioned resolution for {vc_proof_method}; "
            f"credential age {now - vc_proof_created:.1f}s exceeds "
            f"grace window {grace_window:.1f}s. Refusing over-authorization."
        )

    # Reconstruct assertionMethod membership at proof.created.
    authorized = any(
        m.id == vc_proof_method
        and m.purpose == "assertionMethod"
        and m.valid_from <= vc_proof_created
        and (m.valid_until is None or vc_proof_created < m.valid_until)
        for m in historical_methods
    )
    if not authorized:
        raise AttestationBoundaryError(
            f"Signing key {vc_proof_method} was not in assertionMethod at "
            f"proof.created={vc_proof_created}. Purpose-binding failure."
        )

    # Retroactive compromise revocation invalidates credentials signed in
    # the exposure window.
    for r in revocations:
        if r.method_id == vc_proof_method and r.reason == "compromise":
            if r.effective_time <= vc_proof_created:
                raise AttestationBoundaryError(
                    f"Key {vc_proof_method} revoked for compromise at "
                    f"{r.effective_time}, covering proof.created="
                    f"{vc_proof_created}. Treating credential as hostile."
                )
    return True
