from typing import Literal, List, Union, Optional

from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel

from persona_models.common import create_uuid
from persona_models.mcp import HttpTransport, StdioTransport


class McpServerConfiguration(BaseModel):
    name: str
    transport: Union[HttpTransport, StdioTransport]
    enabled_tools: List[str] | None = None
    disabled_tools: List[str] | None = None

    model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)


class FlagConfiguration(BaseModel):
    flag: str

    model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)


FeatureType = Literal["mcp", "flag"]
FeatureConfiguration = Union[McpServerConfiguration, FlagConfiguration]


class FeatureCredentialsConfiguration(BaseModel):
    require_credentials: bool = False
    credentials_type: str | None = None

    model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)


class FeatureTemplate(BaseModel):
    id: str = Field(default_factory=create_uuid, validate_default=False)
    project_id: str | None = None
    type: FeatureType
    name: str
    description: str
    configuration: Optional[FeatureConfiguration] = None
    credentials_configuration: Optional[FeatureCredentialsConfiguration] = None

    model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)


class Feature(BaseModel):
    id: str = Field(default_factory=create_uuid)
    template_id: str | None = None
    credentials_id: str | None = None

    model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)
