from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Text, BigInteger, JSON
from sqlalchemy.orm import relationship

from app.core.database import Base


class DataSource(Base):
    __tablename__ = "data_source"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    source_type = Column(String(50), nullable=False)  # mysql, postgresql, oracle, dm, etc.
    host = Column(String(200))
    port = Column(Integer)
    database_name = Column(String(100))
    username = Column(String(100))
    encrypted_password = Column(Text)
    extra_params = Column(Text)  # JSON string
    status = Column(String(20), default="active")  # active, inactive, error
    dept_id = Column(Integer, ForeignKey("sys_dept.id"), nullable=True)
    created_by = Column(Integer, ForeignKey("sys_user.id"))
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    databases = relationship("Database", back_populates="source", cascade="all, delete-orphan")
    creator = relationship("User")


class Database(Base):
    __tablename__ = "meta_database"

    id = Column(Integer, primary_key=True, index=True)
    source_id = Column(Integer, ForeignKey("data_source.id"), nullable=False)
    name = Column(String(100), nullable=False)
    charset = Column(String(50))
    table_count = Column(Integer, default=0)
    created_at = Column(DateTime, default=datetime.utcnow)
    last_scanned_at = Column(DateTime, nullable=True)
    checksum = Column(String(64), nullable=True)
    is_deleted = Column(Boolean, default=False)
    deleted_at = Column(DateTime, nullable=True)

    source = relationship("DataSource", back_populates="databases")
    tables = relationship("DataTable", back_populates="database", cascade="all, delete-orphan")


class DataTable(Base):
    __tablename__ = "meta_table"

    id = Column(Integer, primary_key=True, index=True)
    database_id = Column(Integer, ForeignKey("meta_database.id"), nullable=False)
    name = Column(String(200), nullable=False)
    comment = Column(String(500))
    row_count = Column(BigInteger, default=0)
    column_count = Column(Integer, default=0)
    created_at = Column(DateTime, default=datetime.utcnow)
    last_scanned_at = Column(DateTime, nullable=True)
    checksum = Column(String(64), nullable=True)
    is_deleted = Column(Boolean, default=False)
    deleted_at = Column(DateTime, nullable=True)

    database = relationship("Database", back_populates="tables")
    columns = relationship("DataColumn", back_populates="table", cascade="all, delete-orphan")


class DataColumn(Base):
    __tablename__ = "meta_column"

    id = Column(Integer, primary_key=True, index=True)
    table_id = Column(Integer, ForeignKey("meta_table.id"), nullable=False)
    name = Column(String(200), nullable=False)
    data_type = Column(String(100))
    length = Column(Integer)
    comment = Column(String(500))
    is_nullable = Column(Boolean, default=True)
    sample_data = Column(Text)  # JSON array of sample values
    created_at = Column(DateTime, default=datetime.utcnow)
    last_scanned_at = Column(DateTime, nullable=True)
    checksum = Column(String(64), nullable=True)
    is_deleted = Column(Boolean, default=False)
    deleted_at = Column(DateTime, nullable=True)

    table = relationship("DataTable", back_populates="columns")


class UnstructuredFile(Base):
    __tablename__ = "unstructured_file"

    id = Column(Integer, primary_key=True, index=True)
    original_name = Column(String(255), nullable=False)
    file_type = Column(String(50))  # word, pdf, txt, excel
    file_size = Column(BigInteger)
    storage_path = Column(String(500))
    extracted_text = Column(Text)
    analysis_result = Column(JSON, nullable=True)  # JSON: {matches: [{rule_name, category, level, snippet}]}
    status = Column(String(20), default="pending")  # pending, processed, error
    created_by = Column(Integer, ForeignKey("sys_user.id"))
    created_at = Column(DateTime, default=datetime.utcnow)
