# [DEF:ConfigModels:Module]
# @TIER: STANDARD
# @SEMANTICS: config, models, pydantic
# @PURPOSE: Defines the data models for application configuration using Pydantic.
# @LAYER: Core
# @RELATION: READS_FROM -> app_configurations (database)
# @RELATION: USED_BY -> ConfigManager

from pydantic import BaseModel, Field
from typing import List, Optional
from ..models.storage import StorageConfig
from ..services.llm_prompt_templates import (
    DEFAULT_LLM_ASSISTANT_SETTINGS,
    DEFAULT_LLM_PROMPTS,
    DEFAULT_LLM_PROVIDER_BINDINGS,
)

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

# [DEF:Environment:DataClass]
# @PURPOSE: 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)
# [/DEF:Environment:DataClass]

# [DEF:LoggingConfig:DataClass]
# @PURPOSE: 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: Optional[str] = None
    max_bytes: int = 10 * 1024 * 1024
    backup_count: int = 5
    enable_belief_state: bool = True
# [/DEF:LoggingConfig:DataClass]

# [DEF:CleanReleaseConfig:DataClass]
# @PURPOSE: Configuration for clean release compliance subsystem.
class CleanReleaseConfig(BaseModel):
    active_policy_id: Optional[str] = None
    active_registry_id: Optional[str] = None
# [/DEF:CleanReleaseConfig:DataClass]

# [DEF:GlobalSettings:DataClass]
# @PURPOSE: Represents global application settings.
class GlobalSettings(BaseModel):
    storage: StorageConfig = Field(default_factory=StorageConfig)
    clean_release: CleanReleaseConfig = Field(default_factory=CleanReleaseConfig)
    default_environment_id: Optional[str] = None
    logging: LoggingConfig = Field(default_factory=LoggingConfig)
    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),
        }
    )
    
    # 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 * * *"
# [/DEF:GlobalSettings:DataClass]

# [DEF:AppConfig:DataClass]
# @PURPOSE: The root configuration model containing all application settings.
class AppConfig(BaseModel):
    environments: List[Environment] = []
    settings: GlobalSettings
# [/DEF:AppConfig:DataClass]

# [/DEF:ConfigModels:Module]
