"""Maintenance tracking models.""" from datetime import datetime from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Index, String, Text, func from sqlalchemy.orm import Mapped, mapped_column, relationship from backend.app.core.database import Base class MaintenanceType(Base): """Defines a type of maintenance task with default interval.""" __tablename__ = "maintenance_types" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(String(100)) description: Mapped[str | None] = mapped_column(Text) default_interval_hours: Mapped[float] = mapped_column(Float, default=100.0) # Interval type: "hours" (print hours) or "days" (calendar days) interval_type: Mapped[str] = mapped_column(String(20), default="hours") icon: Mapped[str | None] = mapped_column(String(50)) # Icon name for UI wiki_url: Mapped[str | None] = mapped_column(String(500)) # Documentation link is_system: Mapped[bool] = mapped_column(Boolean, default=False) # Pre-defined vs custom is_deleted: Mapped[bool] = mapped_column(Boolean, default=False) # Hidden/removed type created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) # Relationships printer_maintenance: Mapped[list["PrinterMaintenance"]] = relationship( back_populates="maintenance_type", cascade="all, delete-orphan" ) class PrinterMaintenance(Base): """Tracks maintenance status for a specific printer.""" __tablename__ = "printer_maintenance" # One row per printer and type: they are created on first read (FO-C14). __table_args__ = (Index("uq_printer_maintenance_printer_type", "printer_id", "maintenance_type_id", unique=True),) id: Mapped[int] = mapped_column(primary_key=True) printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE")) maintenance_type_id: Mapped[int] = mapped_column(ForeignKey("maintenance_types.id", ondelete="CASCADE")) # Custom interval for this printer (overrides default if set) custom_interval_hours: Mapped[float | None] = mapped_column(Float, nullable=True) # Custom interval type for this printer (overrides default if set) custom_interval_type: Mapped[str | None] = mapped_column(String(20), nullable=True) # Tracking enabled: Mapped[bool] = mapped_column(Boolean, default=True) last_performed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) last_performed_hours: Mapped[float] = mapped_column(Float, default=0.0) # Hours at last reset created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now()) # Relationships printer: Mapped["Printer"] = relationship(back_populates="maintenance_items") maintenance_type: Mapped["MaintenanceType"] = relationship(back_populates="printer_maintenance") history: Mapped[list["MaintenanceHistory"]] = relationship( back_populates="printer_maintenance", cascade="all, delete-orphan" ) class MaintenanceHistory(Base): """Log of maintenance actions performed.""" __tablename__ = "maintenance_history" id: Mapped[int] = mapped_column(primary_key=True) printer_maintenance_id: Mapped[int] = mapped_column(ForeignKey("printer_maintenance.id", ondelete="CASCADE")) performed_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) hours_at_maintenance: Mapped[float] = mapped_column(Float, default=0.0) notes: Mapped[str | None] = mapped_column(Text, nullable=True) # Relationships printer_maintenance: Mapped["PrinterMaintenance"] = relationship(back_populates="history") class MaintenanceLogEntry(Base): """One maintenance reset, kept as the audit log of a printer. Unlike ``maintenance_history`` (which goes with its printer task), an entry outlives the printer, the task and the user: their names are stored as they were at the reset, and the links to them are SET NULL on delete. An entry whose printer is gone is still in the fleet-wide export. """ __tablename__ = "maintenance_log_entries" id: Mapped[int] = mapped_column(primary_key=True) printer_id: Mapped[int | None] = mapped_column( ForeignKey("printers.id", ondelete="SET NULL"), nullable=True, index=True ) printer_name: Mapped[str] = mapped_column(String(255)) maintenance_type_id: Mapped[int | None] = mapped_column( ForeignKey("maintenance_types.id", ondelete="SET NULL"), nullable=True, index=True ) maintenance_type_name: Mapped[str] = mapped_column(String(100)) hours_at_maintenance: Mapped[float] = mapped_column(Float, default=0.0) notes: Mapped[str | None] = mapped_column(Text, nullable=True) performed_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True) # NULL when authentication is off. performed_by_username: Mapped[str | None] = mapped_column(String(100), nullable=True) performed_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), index=True) # The maintenance_history row written with it. That row goes with its # printer task, so the link is SET NULL: left dangling, SQLite would give # its id to the next reset, possibly another printer's, and deleting this # entry would delete that reset's history. The upgrade copies history rows # not yet here. history_id: Mapped[int | None] = mapped_column( ForeignKey("maintenance_history.id", ondelete="SET NULL"), nullable=True, index=True ) # Import at end to avoid circular imports from backend.app.models.printer import Printer # noqa: E402