"""Data models for the attestation chain."""

from __future__ import annotations

from dataclasses import dataclass, field


@dataclass(frozen=True)
class EntropyWitnesses:
    """System-state snapshot collected at record creation time.

    Serves as soft evidence that the claimed timestamp is plausible.
    Fabricating convincing witnesses for a backdated record requires
    simulating the full system state at the claimed time.
    """

    sys_uptime: float
    fs_snapshot: bytes  # 16 bytes, truncated SHA-256
    proc_entropy: int
    boot_id: str


@dataclass(frozen=True)
class AttestationChainRecord:
    """A single record in the attestation hash chain.

    Each record wraps content (typically a Verisoo attestation) with
    a hash link to the previous record, entropy witnesses, and an
    Ed25519 signature.
    """

    version: int
    record_id: bytes  # UUID v7, 16 bytes
    chain_index: int
    prev_hash: bytes  # SHA-256, 32 bytes
    content_hash: bytes  # SHA-256 of wrapped content, 32 bytes
    content_type: str
    metadata: dict = field(default_factory=dict)
    claimed_ts: int = 0  # Unix microseconds
    entropy_witnesses: EntropyWitnesses | None = None
    signer_pubkey: bytes = b""  # Ed25519 raw public key, 32 bytes
    signature: bytes = b""  # Ed25519 signature, 64 bytes


@dataclass
class ChainState:
    """Checkpoint of chain state, persisted to state.cbor."""

    chain_id: bytes  # SHA-256 of genesis record
    head_index: int
    head_hash: bytes
    record_count: int
    created_at: int  # Unix µs
    last_append_at: int  # Unix µs
    last_verified_index: int = -1  # Last index that passed verify_chain()

    # Genesis prev_hash sentinel
    GENESIS_PREV_HASH: bytes = b"\x00" * 32
