# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-federated-verifiable-credential-whose-issuer-registry-time-skew-exceeds-the-credential-s-freshness-window.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-federated-verifiable-credential-whose-issuer-registry-time-skew-exceeds-the-credential-s-freshness-window.warrant.json
# Markdown SHA-256:   ce4cc88129c3a62361da3e7ecf07cc70395a242b2487ac9900edf6fd9853d8f6
# Code AST SHA-256:   21610bbd8e35f2985a7deb1c4ae6e40cd4c14687f76b972366b3fac448ef5345
# Verified Classes:   FederatedAttestationError, ClockBound, Credential, StatusSnapshot
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class FederatedAttestationError(RuntimeError):
    """Raised when a federated credential cannot be soundly admitted."""


@dataclass(frozen=True)
class ClockBound:
    registry_id: str
    skew_max_seconds: float


@dataclass(frozen=True)
class Credential:
    issuer_registry: str
    issuance_date: float
    expiration_date: float
    signature_ok: bool


@dataclass(frozen=True)
class StatusSnapshot:
    registry_id: str
    epoch: float          # time the snapshot was published
    revoked: bool


def admit_credential(
    cred: Credential,
    status: StatusSnapshot,
    issuer_clock: ClockBound,
    status_clock: ClockBound,
    now_local: float,
    revalidation_window: float,
) -> str:
    """Return 'admit', 'defer', or 'admit_with_revalidation'.

    Raises FederatedAttestationError on unsound configurations where the
    status snapshot could not have observed the credential's issuance.
    """
    if not cred.signature_ok:
        raise FederatedAttestationError(
            f"signature invalid for credential from {cred.issuer_registry}"
        )

    if status.registry_id != cred.issuer_registry:
        # Cross-registry status resolution is not sound without an
        # explicit trust bridge; refuse rather than guess.
        raise FederatedAttestationError(
            f"status registry {status.registry_id} does not match "
            f"issuer registry {cred.issuer_registry}"
        )

    # Causality clause: the status snapshot must be able to have observed
    # the credential's minting, modulo the status registry's own skew.
    earliest_issuance = cred.issuance_date - issuer_clock.skew_max_seconds
    if status.epoch < earliest_issuance - status_clock.skew_max_seconds:
        raise FederatedAttestationError(
            f"status epoch {status.epoch:.1f} predates earliest possible "
            f"issuance {earliest_issuance:.1f} for {cred.issuer_registry}; "
            f"revocation check is vacuous"
        )

    if status.revoked:
        raise FederatedAttestationError(
            f"credential from {cred.issuer_registry} is revoked as of "
            f"epoch {status.epoch:.1f}"
        )

    # Premature: defer, do not quarantine.
    if now_local < cred.issuance_date - issuer_clock.skew_max_seconds:
        return "defer"

    if now_local > cred.expiration_date + issuer_clock.skew_max_seconds:
        raise FederatedAttestationError(
            f"credential from {cred.issuer_registry} expired at "
            f"{cred.expiration_date:.1f}"
        )

    staleness = now_local - status.epoch
    if staleness > revalidation_window:
        raise FederatedAttestationError(
            f"status view for {cred.issuer_registry} is stale by "
            f"{staleness:.1f}s > revalidation window {revalidation_window:.1f}s"
        )

    return "admit_with_revalidation" if staleness > 0 else "admit"
