# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-did-anchored-verifiable-credential-has-silently-outlived-its-issuer-s-authority-during-cross-org-agent-federation.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-did-anchored-verifiable-credential-has-silently-outlived-its-issuer-s-authority-during-cross-org-agent-federation.warrant.json
# Markdown SHA-256:   6f5dff68ec79578ff00b6cac807eb9797087b46d6e1f0a1a1bbdd104656d53a2
# Code AST SHA-256:   5e81b97c31479fb1276c8c9cae066abe687f258bea57cf7dba8a367497985efa
# Verified Classes:   StaleAuthorityError, DIDDocument, VerifiableCredential
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleAuthorityError(RuntimeError):
    """Raised when a VC's asserted authority is no longer backed by the
    issuer DID's current assertionMethod set, or when the verifier cannot
    establish freshness within its staleness bound."""
    pass


@dataclass
class DIDDocument:
    did: str
    version_id: str
    assertion_method: set[str]
    resolved_at: float


@dataclass
class VerifiableCredential:
    issuer_did: str
    verification_method: str  # e.g. "did:web:issuer.y.example#key-2"
    subject_did: str
    capability: str
    expiration: float
    status_list_url: str
    status_index: int


def verify_presentation(
    vc: VerifiableCredential,
    resolver,
    status_fetcher,
    max_did_staleness_s: float = 60.0,
    max_status_staleness_s: float = 300.0,
    now: float | None = None,
) -> bool:
    now = time.time() if now is None else now

    # 1. Signature validity is assumed already checked by the caller's
    #    crypto layer against the resolved key. We do NOT re-check it here.

    # 2. Authorization validity: re-resolve the DID at presentation time.
    doc: DIDDocument = resolver.resolve(vc.issuer_did)
    did_age = now - doc.resolved_at
    if did_age > max_did_staleness_s:
        raise StaleAuthorityError(
            f"DID document for {vc.issuer_did} is {did_age:.1f}s stale "
            f"(bound {max_did_staleness_s}s); cannot confirm current "
            f"assertionMethod membership for {vc.verification_method}"
        )
    if vc.verification_method not in doc.assertion_method:
        raise StaleAuthorityError(
            f"{vc.verification_method} is no longer in assertionMethod of "
            f"{vc.issuer_did} (version {doc.version_id}); issuer authority "
            f"for capability {vc.capability!r} has drifted since issuance"
        )

    # 3. Revocation validity: fresh status fetch, bounded staleness.
    status, fetched_at = status_fetcher.fetch(
        vc.status_list_url, vc.status_index
    )
    status_age = now - fetched_at
    if status_age > max_status_staleness_s:
        raise StaleAuthorityError(
            f"Status list {vc.status_list_url} is {status_age:.1f}s stale "
            f"(bound {max_status_staleness_s}s); revocation state unknown"
        )
    if status == "revoked":
        raise StaleAuthorityError(
            f"Credential for {vc.subject_did} revoked at status index "
            f"{vc.status_index}; capability {vc.capability!r} is a ghost"
        )

    # 4. Expiry is the weakest check and must not substitute for 2 and 3.
    if now >= vc.expiration:
        raise StaleAuthorityError("credential expired")

    return True
