# #region DatasetReviewSchemaComposites [C:2] [TYPE Module] [SEMANTICS pydantic, dataset, review, composite, schema]
# @BRIEF Composite Pydantic DTOs for clarification, preview, run context, and session summary/detail responses.
# @LAYER API
# @RELATION DEPENDS_ON -> [DatasetReviewSchemaDtos]

from datetime import datetime
from typing import Any

from pydantic import BaseModel, ConfigDict

from src.models.dataset_review import (
    AnswerKind,
    ClarificationStatus,
    LaunchStatus,
    PreviewStatus,
    QuestionState,
    ReadinessState,
    RecommendedAction,
    SessionPhase,
    SessionStatus,
)
from src.schemas.dataset_review_pkg._dtos import (
    DatasetProfileDto,
    ExecutionMappingDto,
    ImportedFilterDto,
    SemanticFieldEntryDto,
    SemanticSourceDto,
    SessionCollaboratorDto,
    TemplateVariableDto,
    ValidationFindingDto,
)


# #region ClarificationOptionDto [C:1] [TYPE Class]
# @BRIEF Clarification option DTO.
class ClarificationOptionDto(BaseModel):
    option_id: str
    question_id: str
    label: str
    value: str
    is_recommended: bool
    display_order: int

    model_config = ConfigDict(from_attributes=True)


# #endregion ClarificationOptionDto


# #region ClarificationAnswerDto [C:1] [TYPE Class]
# @BRIEF Clarification answer DTO with feedback.
class ClarificationAnswerDto(BaseModel):
    answer_id: str
    question_id: str
    answer_kind: AnswerKind
    answer_value: str | None = None
    answered_by_user_id: str
    impact_summary: str | None = None
    user_feedback: str | None = None
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion ClarificationAnswerDto


# #region ClarificationQuestionDto [C:2] [TYPE Class]
# @BRIEF Clarification question DTO with nested options and answer.
class ClarificationQuestionDto(BaseModel):
    question_id: str
    clarification_session_id: str
    topic_ref: str
    question_text: str
    why_it_matters: str
    current_guess: str | None = None
    priority: int
    state: QuestionState
    created_at: datetime
    updated_at: datetime
    options: list[ClarificationOptionDto] = []
    answer: ClarificationAnswerDto | None = None

    model_config = ConfigDict(from_attributes=True)


# #endregion ClarificationQuestionDto


# #region ClarificationSessionDto [C:2] [TYPE Class]
# @BRIEF Clarification session DTO with nested questions.
class ClarificationSessionDto(BaseModel):
    clarification_session_id: str
    session_id: str
    status: ClarificationStatus
    current_question_id: str | None = None
    resolved_count: int
    remaining_count: int
    summary_delta: str | None = None
    started_at: datetime
    updated_at: datetime
    completed_at: datetime | None = None
    questions: list[ClarificationQuestionDto] = []

    model_config = ConfigDict(from_attributes=True)


# #endregion ClarificationSessionDto


# #region CompiledPreviewDto [C:1] [TYPE Class]
# @BRIEF Compiled preview DTO with fingerprint and session version.
class CompiledPreviewDto(BaseModel):
    preview_id: str
    session_id: str
    session_version: int | None = None
    preview_status: PreviewStatus
    compiled_sql: str | None = None
    preview_fingerprint: str
    compiled_by: str
    error_code: str | None = None
    error_details: str | None = None
    compiled_at: datetime | None = None
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion CompiledPreviewDto


# #region DatasetRunContextDto [C:1] [TYPE Class]
# @BRIEF Run context DTO with launch audit data and session version.
class DatasetRunContextDto(BaseModel):
    run_context_id: str
    session_id: str
    session_version: int | None = None
    dataset_ref: str
    environment_id: str
    preview_id: str
    sql_lab_session_ref: str
    effective_filters: Any
    template_params: Any
    approved_mapping_ids: list[str]
    semantic_decision_refs: list[str]
    open_warning_refs: list[str]
    launch_status: LaunchStatus
    launch_error: str | None = None
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion DatasetRunContextDto


# #region SessionSummary [C:2] [TYPE Class]
# @BRIEF Lightweight session summary DTO for list responses.
class SessionSummary(BaseModel):
    session_id: str
    user_id: str
    environment_id: str
    source_kind: str
    source_input: str
    dataset_ref: str
    dataset_id: int | None = None
    version: int = 0
    session_version: int = 0
    readiness_state: ReadinessState
    recommended_action: RecommendedAction
    status: SessionStatus
    current_phase: SessionPhase
    created_at: datetime
    updated_at: datetime
    last_activity_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion SessionSummary


# #region SessionDetail [C:2] [TYPE Class]
# @BRIEF Full session detail DTO with all nested aggregates for detail views.
# @RELATION INHERITS -> [SessionSummary]
class SessionDetail(SessionSummary):
    collaborators: list[SessionCollaboratorDto] = []
    profile: DatasetProfileDto | None = None
    findings: list[ValidationFindingDto] = []
    semantic_sources: list[SemanticSourceDto] = []
    semantic_fields: list[SemanticFieldEntryDto] = []
    imported_filters: list[ImportedFilterDto] = []
    template_variables: list[TemplateVariableDto] = []
    execution_mappings: list[ExecutionMappingDto] = []
    clarification_sessions: list[ClarificationSessionDto] = []
    previews: list[CompiledPreviewDto] = []
    run_contexts: list[DatasetRunContextDto] = []


# #endregion SessionDetail


# #endregion DatasetReviewSchemaComposites
