"""Asset Model"""
from sqlalchemy import Column, String, Boolean, DateTime, ForeignKey, Text, Integer, BigInteger, Numeric
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
import uuid
from app.database import Base


class Asset(Base):
    __tablename__ = "assets"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    user_id = Column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"))
    project_id = Column(UUID(as_uuid=True), ForeignKey("projects.id", ondelete="SET NULL"))

    # File information
    original_filename = Column(String(500))
    stored_filename = Column(String(500), nullable=False)
    file_path = Column(Text, nullable=False)
    thumbnail_path = Column(Text)  # Proxy thumbnail for fast UI loading
    file_type = Column(String(50), nullable=False)  # image, video, audio, document
    mime_type = Column(String(100))
    file_size_bytes = Column(BigInteger)

    # Metadata
    width = Column(Integer)
    height = Column(Integer)
    duration_seconds = Column(Numeric(10, 2))
    asset_metadata = Column('metadata', JSONB, default={})

    # Source tracking
    source_module = Column(String(100))
    source_job_id = Column(UUID(as_uuid=True))
    parent_asset_id = Column(UUID(as_uuid=True), ForeignKey("assets.id"))

    # Status
    is_temporary = Column(Boolean, default=False)
    expires_at = Column(DateTime(timezone=True))

    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # Relationships
    user = relationship("User", back_populates="assets")
    project = relationship("Project", back_populates="assets")
    parent = relationship("Asset", remote_side=[id])
