from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, JSON, String
from sqlalchemy.sql import func

from app.db.session import Base


class DataSnapshot(Base):
    __tablename__ = "data_snapshots"

    id = Column(Integer, primary_key=True, autoincrement=True)
    datasource_id = Column(Integer, nullable=False, index=True)
    task_id = Column(Integer, ForeignKey("collection_tasks.id"), nullable=True, index=True)
    source = Column(String(100), nullable=False, index=True)
    snapshot_key = Column(String(100), nullable=True, index=True)
    reference_date = Column(DateTime(timezone=True), nullable=True)
    started_at = Column(DateTime(timezone=True), server_default=func.now())
    completed_at = Column(DateTime(timezone=True), nullable=True)
    record_count = Column(Integer, default=0)
    status = Column(String(20), nullable=False, default="running")
    is_current = Column(Boolean, default=True, index=True)
    parent_snapshot_id = Column(Integer, ForeignKey("data_snapshots.id"), nullable=True, index=True)
    summary = Column(JSON, default={})
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    def __repr__(self):
        return f"<DataSnapshot {self.id}: {self.source}/{self.status}>"
