# #region DatasetReviewSchemaDtos [C:2] [TYPE Module] [SEMANTICS pydantic, dataset, review, dto, schema]
# @BRIEF Pydantic DTOs for session, profile, findings, collaborators, and semantic field API payloads.
# @LAYER API
# @RELATION DEPENDS_ON -> [DatasetReviewModels]

from datetime import datetime
from typing import Any

from pydantic import BaseModel, ConfigDict

from src.models.dataset_review import (
    ApprovalState,
    BusinessSummarySource,
    CandidateMatchType,
    CandidateStatus,
    ConfidenceState,
    FieldKind,
    FieldProvenance,
    FilterConfidenceState,
    FilterRecoveryStatus,
    FilterSource,
    FindingArea,
    FindingSeverity,
    MappingMethod,
    MappingStatus,
    MappingWarningLevel,
    ResolutionState,
    SemanticSourceStatus,
    SemanticSourceType,
    SessionCollaboratorRole,
    TrustLevel,
    VariableKind,
)


# #region SessionCollaboratorDto [C:1] [TYPE Class]
# @BRIEF Collaborator DTO for session access control.
class SessionCollaboratorDto(BaseModel):
    user_id: str
    role: SessionCollaboratorRole
    added_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion SessionCollaboratorDto


# #region DatasetProfileDto [C:1] [TYPE Class]
# @BRIEF Dataset profile DTO with business summary and confidence metadata.
class DatasetProfileDto(BaseModel):
    profile_id: str
    session_id: str
    dataset_name: str
    schema_name: str | None = None
    database_name: str | None = None
    business_summary: str
    business_summary_source: BusinessSummarySource
    description: str | None = None
    dataset_type: str | None = None
    is_sqllab_view: bool
    completeness_score: float | None = None
    confidence_state: ConfidenceState
    has_blocking_findings: bool
    has_warning_findings: bool
    manual_summary_locked: bool
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion DatasetProfileDto


# #region ValidationFindingDto [C:1] [TYPE Class]
# @BRIEF Validation finding DTO with resolution tracking.
class ValidationFindingDto(BaseModel):
    finding_id: str
    session_id: str
    area: FindingArea
    severity: FindingSeverity
    code: str
    title: str
    message: str
    resolution_state: ResolutionState
    resolution_note: str | None = None
    caused_by_ref: str | None = None
    created_at: datetime
    resolved_at: datetime | None = None

    model_config = ConfigDict(from_attributes=True)


# #endregion ValidationFindingDto


# #region SemanticSourceDto [C:1] [TYPE Class]
# @BRIEF Semantic source DTO with trust level and status.
class SemanticSourceDto(BaseModel):
    source_id: str
    session_id: str
    source_type: SemanticSourceType
    source_ref: str
    source_version: str
    display_name: str
    trust_level: TrustLevel
    schema_overlap_score: float | None = None
    status: SemanticSourceStatus
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion SemanticSourceDto


# #region SemanticCandidateDto [C:1] [TYPE Class]
# @BRIEF Semantic candidate DTO with match type and confidence score.
class SemanticCandidateDto(BaseModel):
    candidate_id: str
    field_id: str
    source_id: str | None = None
    candidate_rank: int
    match_type: CandidateMatchType
    confidence_score: float
    proposed_verbose_name: str | None = None
    proposed_description: str | None = None
    proposed_display_format: str | None = None
    status: CandidateStatus
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion SemanticCandidateDto


# #region SemanticFieldEntryDto [C:2] [TYPE Class]
# @BRIEF Semantic field entry DTO with nested candidates and session version.
class SemanticFieldEntryDto(BaseModel):
    field_id: str
    session_id: str
    session_version: int | None = None
    field_name: str
    field_kind: FieldKind
    verbose_name: str | None = None
    description: str | None = None
    display_format: str | None = None
    provenance: FieldProvenance
    source_id: str | None = None
    source_version: str | None = None
    confidence_rank: int | None = None
    is_locked: bool
    has_conflict: bool
    needs_review: bool
    last_changed_by: str
    user_feedback: str | None = None
    created_at: datetime
    updated_at: datetime
    candidates: list[SemanticCandidateDto] = []

    model_config = ConfigDict(from_attributes=True)


# #endregion SemanticFieldEntryDto


# #region ImportedFilterDto [C:1] [TYPE Class]
# @BRIEF Imported filter DTO with confidence and recovery status.
class ImportedFilterDto(BaseModel):
    filter_id: str
    session_id: str
    filter_name: str
    display_name: str | None = None
    raw_value: Any
    raw_value_masked: bool = False
    normalized_value: Any | None = None
    source: FilterSource
    confidence_state: FilterConfidenceState
    requires_confirmation: bool
    recovery_status: FilterRecoveryStatus
    notes: str | None = None
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion ImportedFilterDto


# #region TemplateVariableDto [C:1] [TYPE Class]
# @BRIEF Template variable DTO with mapping status.
class TemplateVariableDto(BaseModel):
    variable_id: str
    session_id: str
    variable_name: str
    expression_source: str
    variable_kind: VariableKind
    is_required: bool
    default_value: Any | None = None
    mapping_status: MappingStatus
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion TemplateVariableDto


# #region ExecutionMappingDto [C:1] [TYPE Class]
# @BRIEF Execution mapping DTO with approval state and session version.
class ExecutionMappingDto(BaseModel):
    mapping_id: str
    session_id: str
    session_version: int | None = None
    filter_id: str
    variable_id: str
    mapping_method: MappingMethod
    raw_input_value: Any
    effective_value: Any | None = None
    transformation_note: str | None = None
    warning_level: MappingWarningLevel | None = None
    requires_explicit_approval: bool
    approval_state: ApprovalState
    approved_by_user_id: str | None = None
    approved_at: datetime | None = None
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


# #endregion ExecutionMappingDto


# #endregion DatasetReviewSchemaDtos
