"""Data Source model"""

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

from app.db.session import Base


class DataSource(Base):
    __tablename__ = "data_sources"

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), nullable=False)
    source = Column(String(100), nullable=False)
    module = Column(String(10), nullable=False, index=True)  # L1, L2, L3, L4
    priority = Column(String(10), default="P1")  # P0, P1, P2
    frequency_minutes = Column(Integer, default=60)
    collector_class = Column(String(100), nullable=False)
    config = Column(Text, default="{}")  # JSON config
    is_active = Column(Boolean, default=True, index=True)
    last_run_at = Column(DateTime(timezone=True))
    last_status = Column(String(20))
    next_run_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())

    def __repr__(self):
        return f"<DataSource {self.id}: {self.name}>"
