"""Plan settings domain models for CleverAgents.

Based on Phase 0 discovery stubs from plan_model_settings.py.
"""

from __future__ import annotations

from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field

from ..aimodels_custom import CustomModel, CustomProvider
from ..aimodelsdatamodels import BaseModelUsesProvider, ModelPack


def _empty_model_pack_list() -> list[ModelPack]:
    return []


def _empty_custom_model_list() -> list[CustomModel]:
    return []


def _empty_custom_provider_list() -> list[CustomProvider]:
    return []


def _empty_custom_models_by_id() -> dict[str, CustomModel]:
    return {}


def _empty_uses_custom_provider_by_model_id() -> dict[str, list[BaseModelUsesProvider]]:
    return {}


__all__ = ["PlanSettings"]


class PlanSettings(BaseModel):
    """Data contract for PlanSettings.

    Tracks the model configuration for a plan, including:
    - The active model pack
    - Custom model packs defined by the user
    - Custom models and providers
    - Whether this is a cloud deployment
    """

    model_pack_name: str = Field(..., alias="modelPackName")
    model_pack: ModelPack | None = Field(default=None, alias="modelPack")
    custom_model_packs: list[ModelPack] = Field(
        default_factory=_empty_model_pack_list, alias="customModelPacks"
    )
    custom_models: list[CustomModel] = Field(
        default_factory=_empty_custom_model_list, alias="customModels"
    )
    custom_models_by_id: dict[str, CustomModel] = Field(
        default_factory=_empty_custom_models_by_id, alias="customModelsById"
    )
    custom_providers: list[CustomProvider] = Field(
        default_factory=_empty_custom_provider_list, alias="customProviders"
    )
    uses_custom_provider_by_model_id: dict[str, list[BaseModelUsesProvider]] = Field(
        default_factory=_empty_uses_custom_provider_by_model_id,
        alias="usesCustomProviderByModelId",
    )
    is_cloud: bool = Field(False, alias="isCloud")
    configured: bool = Field(False)
    updated_at: datetime | None = Field(default=None, alias="updatedAt")

    model_config = ConfigDict(
        str_strip_whitespace=True,
        validate_assignment=True,
        arbitrary_types_allowed=False,
        populate_by_name=True,
        use_enum_values=True,
    )
