"""Pydantic models for F5 BIG-IP iControl REST API tools."""

import os
from datetime import UTC, datetime

from pydantic import BaseModel, Field

from mcp_network_jcd.models.paloalto import ErrorInfo

F5_OBJECT_TYPES = frozenset({"virtual", "pool", "irule", "profile"})

F5_SUBCOLLECTIONS: dict[str, frozenset[str]] = {
    "pool": frozenset({"members"}),
    "virtual": frozenset({"policies", "profiles"}),
}


class F5Credentials(BaseModel):
    """Credentials loaded from environment variables for F5 iControl REST API."""

    username: str = Field(..., min_length=1)
    password: str = Field(..., min_length=1, repr=False)

    @classmethod
    def from_env(cls) -> "F5Credentials":
        """Load credentials from environment variables."""
        username = os.environ.get("F5_USERNAME", "")
        password = os.environ.get("F5_PASSWORD", "")
        if not username:
            raise ValueError("F5_USERNAME must be set")
        if not password:
            raise ValueError("F5_PASSWORD must be set")
        return cls(username=username, password=password)


class F5Response(BaseModel):
    """Response from an F5 iControl REST API call."""

    success: bool
    host: str
    data: dict | str | None = None
    error: ErrorInfo | None = None
    duration_ms: int = Field(ge=0)
    timestamp: str = Field(
        default_factory=lambda: datetime.now(UTC).isoformat().replace("+00:00", "Z")
    )
