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


class SchemaChangeLog(Base):
    __tablename__ = "schema_change_log"

    id = Column(Integer, primary_key=True, index=True)
    source_id = Column(Integer, ForeignKey("data_source.id"), nullable=False)
    database_id = Column(Integer, ForeignKey("meta_database.id"), nullable=True)
    table_id = Column(Integer, ForeignKey("meta_table.id"), nullable=True)
    column_id = Column(Integer, ForeignKey("meta_column.id"), nullable=True)
    change_type = Column(String(20), nullable=False)  # add_table, drop_table, add_column, drop_column, change_type, change_comment
    old_value = Column(Text)
    new_value = Column(Text)
    detected_at = Column(DateTime, default=datetime.utcnow)

    source = relationship("DataSource")
    database = relationship("Database")
    table = relationship("DataTable")
    column = relationship("DataColumn")
