from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, BigInteger
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.core.database import Base


class ClientDistribution(Base):
    __tablename__ = "client_distributions"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)
    icon_url = Column(String(255), nullable=True)
    owner_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    is_deleted = Column(Integer, default=0, nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now(), server_default=func.now())

    owner = relationship("User")
    versions = relationship("ClientVersion", back_populates="distribution", cascade="all, delete-orphan")


class ClientVersion(Base):
    __tablename__ = "client_versions"

    id = Column(Integer, primary_key=True, index=True)
    distribution_id = Column(Integer, ForeignKey("client_distributions.id", ondelete="CASCADE"), nullable=False)
    version_code = Column(String(32), nullable=False)
    version_name = Column(String(100), nullable=True)
    release_notes = Column(Text, nullable=True)
    object_key = Column(String(512), nullable=False)
    file_size = Column(BigInteger, nullable=True)
    platform = Column(String(32), nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    distribution = relationship("ClientDistribution", back_populates="versions")
