""" Traceability — Hash Record Models Stores the append-only log of on-chain-worthy integrity proofs. PHI LEVEL: NONE — hashes only, no patient data. Multi-tenant: tenant_id on every record (Commandment #3). """ from __future__ import annotations from datetime import UTC, datetime, timezone from enum import Enum from typing import Any, Dict, Optional from uuid import UUID, uuid4 from sqlalchemy import JSON from sqlmodel import Column, Field, SQLModel class HashRecordType(str, Enum): """What the hash represents. Never store PHI — only typed references.""" PROTOCOL_VERSION = "protocol_version" # surgical/clinical protocol snapshot WORKFLOW_SIGNATURE = "workflow_signature" # who executed what workflow, when AUDIT_NOTARIZATION = "audit_notarization" # audit trail export integrity proof PUBLICATION_TIMESTAMP = "publication_timestamp" # research dataset anchor CONSENT_PROOF = "consent_proof" # consent given — hash only, no PHI DATASET_INTEGRITY = "dataset_integrity" # anonymized export fingerprint INSTITUTIONAL_VALIDATION = "institutional_validation" # partner trust record CASE_EXPORT = "case_export" # de-identified case export proof class HashRecord(SQLModel, table=True): """ Append-only integrity proof record. SECURITY: Never store PHI here. Hash inputs must be pre-sanitized. Tenant isolation: tenant_id mandatory (Commandment #3). Sprint: 72 """ __tablename__ = "traceability_hash_records" id: UUID = Field(default_factory=uuid4, primary_key=True) tenant_id: UUID = Field(index=True) record_type: HashRecordType sha256_hash: str = Field(max_length=64) # hex SHA-256 reference_id: str | None = Field(default=None, max_length=200) # e.g. case_id, protocol_slug reference_type: str | None = Field( default=None, max_length=100 ) # e.g. "surgio_case", "protocol" actor_id: UUID | None = Field(default=None) # who triggered this engine: str | None = Field(default=None, max_length=50) # e.g. "surgio", "educa" extra: dict[str, Any] | None = Field( default=None, sa_column=Column(JSON) ) # was 'metadata' — reserved by SQLAlchemy created_at: datetime = Field(default_factory=lambda: datetime.now(UTC)) class Config: use_enum_values = True