# #region DatasetReviewFilterModels [C:3] [TYPE Module] [SEMANTICS sqlalchemy, dataset, review, filter, template]
# @BRIEF Imported filter and template variable models for Superset context recovery and execution mapping.
# @LAYER Domain
# @RELATION DEPENDS_ON -> [DatasetReviewEnums]
# @RELATION DEPENDS_ON -> [MappingModels]

from datetime import UTC, datetime
import uuid

from sqlalchemy import (
    JSON,
    Boolean,
    Column,
    DateTime,
    Enum as SQLEnum,
    ForeignKey,
    String,
    Text,
)
from sqlalchemy.orm import relationship

from src.models.dataset_review_pkg._enums import (
    FilterConfidenceState,
    FilterRecoveryStatus,
    FilterSource,
    MappingStatus,
    VariableKind,
)
from src.models.mapping import Base


# #region ImportedFilter [C:2] [TYPE Class]
# @BRIEF Recovered Superset filter with confidence and recovery status tracking.
# @RELATION DEPENDS_ON -> [DatasetReviewSession]
class ImportedFilter(Base):
    __tablename__ = "imported_filters"

    filter_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    session_id = Column(
        String, ForeignKey("dataset_review_sessions.session_id"), nullable=False
    )
    filter_name = Column(String, nullable=False)
    display_name = Column(String, nullable=True)
    raw_value = Column(JSON, nullable=False)
    raw_value_masked = Column(Boolean, nullable=False, default=False)
    normalized_value = Column(JSON, nullable=True)
    source = Column(SQLEnum(FilterSource), nullable=False)
    confidence_state = Column(SQLEnum(FilterConfidenceState), nullable=False)
    requires_confirmation = Column(Boolean, nullable=False, default=False)
    recovery_status = Column(SQLEnum(FilterRecoveryStatus), nullable=False)
    notes = Column(Text, nullable=True)
    created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)
    updated_at = Column(
        DateTime, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC), nullable=False
    )

    session = relationship("DatasetReviewSession", back_populates="imported_filters")


# #endregion ImportedFilter


# #region TemplateVariable [C:2] [TYPE Class]
# @BRIEF Discovered template variable from dataset SQL with mapping status tracking.
# @RELATION DEPENDS_ON -> [DatasetReviewSession]
class TemplateVariable(Base):
    __tablename__ = "template_variables"

    variable_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    session_id = Column(
        String, ForeignKey("dataset_review_sessions.session_id"), nullable=False
    )
    variable_name = Column(String, nullable=False)
    expression_source = Column(Text, nullable=False)
    variable_kind = Column(SQLEnum(VariableKind), nullable=False)
    is_required = Column(Boolean, nullable=False, default=True)
    default_value = Column(JSON, nullable=True)
    mapping_status = Column(
        SQLEnum(MappingStatus), nullable=False, default=MappingStatus.UNMAPPED
    )
    created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)
    updated_at = Column(
        DateTime, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC), nullable=False
    )

    session = relationship("DatasetReviewSession", back_populates="template_variables")


# #endregion TemplateVariable


# #endregion DatasetReviewFilterModels
