"""Equipment cache model (imported from Rentman)."""
from sqlalchemy import Column, Integer, String, Text, Boolean, DECIMAL, JSON, Index, TIMESTAMP, func
from app.database import Base


class EquipmentCache(Base):
    __tablename__ = "equipment_cache"

    id = Column(Integer, primary_key=True, autoincrement=True)
    rentman_id = Column(String(64), unique=True, nullable=False)
    name = Column(String(255), nullable=False)
    number = Column(String(64))
    category = Column(String(128))
    subcategory = Column(String(128))
    description = Column(Text)
    specifications = Column(JSON)
    images = Column(JSON)
    update_hash = Column(String(128))
    rental_price = Column(DECIMAL(10, 2))
    brand = Column(String(128))
    available = Column(Boolean, default=True)
    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())

    @property
    def image_url(self) -> str | None:
        """Return local image URL if images exist, else None."""
        if self.images and len(self.images) > 0:
            return f"/api/equipment/{self.id}/image"
        return None

    __table_args__ = (
        Index("idx_equipment_category", "category"),
        Index("idx_equipment_name", "name"),
        Index("idx_equipment_number", "number"),
    )
