# #region ConfigModels [C:3] [TYPE Module] [SEMANTICS pydantic, model, schedule]
# @BRIEF Defines the data models for application configuration using Pydantic.
# @LAYER Core
# @RELATION IMPLEMENTS -> [CoreContracts]
# @RELATION IMPLEMENTS -> [ConnectionContracts]
#
# #region CoreContracts [C:2] [TYPE Interface]
# @BRIEF Contract for core configuration models — Pydantic BaseModel subclasses with scheduler config.
# #endregion CoreContracts
#
# #region ConnectionContracts [C:2] [TYPE Interface]
# @BRIEF Contract for database/environment connection configuration models.
# #endregion ConnectionContracts

from pydantic import BaseModel, Field, field_validator

from ..models.storage import StorageConfig
from ..services.llm_prompt_templates import (
    DEFAULT_LLM_ASSISTANT_SETTINGS,
    DEFAULT_LLM_PROMPTS,
    DEFAULT_LLM_PROVIDER_BINDINGS,
)


# #region Schedule [TYPE DataClass]
# @BRIEF Represents a backup schedule configuration.
class Schedule(BaseModel):
    enabled: bool = False
    cron_expression: str = "0 0 * * *"  # Default: daily at midnight


# #endregion Schedule


# #region Environment [TYPE DataClass]
# @BRIEF Represents a Superset environment configuration.
class Environment(BaseModel):
    id: str
    name: str
    url: str
    username: str
    password: str  # Will be masked in UI
    stage: str = Field(default="DEV", pattern="^(DEV|PREPROD|PROD)$")
    verify_ssl: bool = True
    timeout: int = 30
    is_default: bool = False
    is_production: bool = False
    backup_schedule: Schedule = Field(default_factory=Schedule)


# #endregion Environment


# #region LoggingConfig [TYPE DataClass]
# @BRIEF Defines the configuration for the application's logging system.
class LoggingConfig(BaseModel):
    level: str = "INFO"
    task_log_level: str = (
        "INFO"  # Minimum level for task-specific logs (DEBUG, INFO, WARNING, ERROR)
    )
    file_path: str | None = None
    max_bytes: int = 10 * 1024 * 1024
    backup_count: int = 5
    enable_belief_state: bool = True


# #endregion LoggingConfig


# #region CleanReleaseConfig [TYPE DataClass]
# @BRIEF Configuration for clean release compliance subsystem.
class CleanReleaseConfig(BaseModel):
    active_policy_id: str | None = None
    active_registry_id: str | None = None


# #endregion CleanReleaseConfig


# #region FeaturesConfig [C:1] [TYPE DataClass]
# @BRIEF Top-level feature flags that toggle entire project features on/off.
# @RATIONALE  Features are read from environment variables on bootstrap and persisted in DB.
#            DB is source of truth after initial bootstrap; env vars only seed defaults.
class FeaturesConfig(BaseModel):
    dataset_review: bool = True
    health_monitor: bool = True


# #endregion FeaturesConfig


# #region GlobalSettings [TYPE DataClass]
# @BRIEF Represents global application settings.
class GlobalSettings(BaseModel):
    storage: StorageConfig = Field(default_factory=StorageConfig)
    clean_release: CleanReleaseConfig = Field(default_factory=CleanReleaseConfig)
    default_environment_id: str | None = None
    logging: LoggingConfig = Field(default_factory=LoggingConfig)
    features: FeaturesConfig = Field(default_factory=FeaturesConfig)
    connections: list[dict] = []
    llm: dict = Field(
        default_factory=lambda: {
            "providers": [],
            "default_provider": "",
            "prompts": dict(DEFAULT_LLM_PROMPTS),
            "provider_bindings": dict(DEFAULT_LLM_PROVIDER_BINDINGS),
            **dict(DEFAULT_LLM_ASSISTANT_SETTINGS),
        }
    )

    # Application timezone
    app_timezone: str = "Europe/Moscow"

    @field_validator("app_timezone")
    @classmethod
    def validate_app_timezone(cls, v: str) -> str:
        from zoneinfo import ZoneInfo
        try:
            ZoneInfo(v)
        except (KeyError, TypeError):
            raise ValueError(f"Invalid IANA timezone: {v}") from None
        return v

    # Task retention settings
    task_retention_days: int = 30
    task_retention_limit: int = 100
    pagination_limit: int = 10

    # Migration sync settings
    migration_sync_cron: str = "0 2 * * *"

    # Dataset Review Feature Flags
    ff_dataset_auto_review: bool = True
    ff_dataset_clarification: bool = True
    ff_dataset_execution: bool = True

    # LLM validation retention (days)
    LLM_SCREENSHOT_RETENTION_DAYS: int = 30
    LLM_RAW_RESPONSE_RETENTION_DAYS: int = 30

    # Global worker limit for concurrent validation runs
    GLOBAL_VALIDATION_WORKER_LIMIT: int = 3


# #endregion GlobalSettings


# #region AppConfig [TYPE DataClass]
# @BRIEF The root configuration model containing all application settings.
class AppConfig(BaseModel):
    environments: list[Environment] = []
    settings: GlobalSettings


# #endregion AppConfig

# #endregion ConfigModels
