import uuid from datetime import datetime, timezone from sqlalchemy import DateTime, ForeignKey, String from sqlalchemy.orm import Mapped, mapped_column from app.db import Base class Device(Base): """A paired hardware sensor node (ESP32-P4 etc.), belonging to exactly one user. Authenticates telemetry via a bearer token whose hash — never the raw value — is stored here, identical convention to `AuthSession.token_hash` (see `app/models/auth_session.py`).""" __tablename__ = "devices" id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4) user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"), index=True) name: Mapped[str] = mapped_column(String(64)) token_hash: Mapped[str] = mapped_column(String(64), unique=True, index=True) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=lambda: datetime.now(timezone.utc) ) last_seen_at: Mapped[datetime | None] = mapped_column( DateTime(timezone=True), nullable=True )