import uuid from datetime import datetime from sqlalchemy import String, DateTime, Boolean, Text, ForeignKey from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.dialects.postgresql import UUID, JSONB from app.database import Base class Product(Base): __tablename__ = "products" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) pim_id: Mapped[str] = mapped_column(String(500), nullable=False) name: Mapped[str | None] = mapped_column(String(500), nullable=True) category_key: Mapped[str | None] = mapped_column(String(100), nullable=True, index=True) ebene1: Mapped[str | None] = mapped_column(String(500), nullable=True) ebene2: Mapped[str | None] = mapped_column(String(500), nullable=True) baureihe: Mapped[str | None] = mapped_column(String(500), nullable=True) produkt_baureihe: Mapped[str | None] = mapped_column(String(500), nullable=True) lagertyp: Mapped[str | None] = mapped_column(String(500), nullable=True) name_cad_modell: Mapped[str | None] = mapped_column(String(500), nullable=True, index=True) gewuenschte_bildnummer: Mapped[str | None] = mapped_column(String(500), nullable=True) medias_rendering: Mapped[bool | None] = mapped_column(Boolean, nullable=True) components: Mapped[list] = mapped_column(JSONB, nullable=False, default=list) cad_part_materials: Mapped[list] = mapped_column(JSONB, nullable=False, default=list) cad_file_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("cad_files.id", ondelete="SET NULL"), nullable=True ) notes: Mapped[str | None] = mapped_column(Text, nullable=True) is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True) arbeitspaket: Mapped[str | None] = mapped_column(String(500), nullable=True) source_excel: Mapped[str | None] = mapped_column(String(1000), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False) updated_at: Mapped[datetime] = mapped_column( DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False ) cad_file: Mapped["CadFile | None"] = relationship("CadFile", back_populates="products") order_lines: Mapped[list["OrderLine"]] = relationship( "OrderLine", back_populates="product", cascade="all, delete-orphan" ) render_positions: Mapped[list["ProductRenderPosition"]] = relationship( "ProductRenderPosition", back_populates="product", cascade="all, delete-orphan", order_by="ProductRenderPosition.sort_order" ) @property def thumbnail_url(self) -> str | None: if self.cad_file and self.cad_file.thumbnail_path: from pathlib import Path return f"/thumbnails/{Path(self.cad_file.thumbnail_path).name}" return None @property def processing_status(self) -> str | None: if self.cad_file: return self.cad_file.processing_status.value if hasattr( self.cad_file.processing_status, 'value' ) else str(self.cad_file.processing_status) return None @property def cad_parsed_objects(self) -> list[str] | None: if self.cad_file and self.cad_file.parsed_objects: return self.cad_file.parsed_objects.get("objects") or [] return None