"""
Agent entity models.
"""

from pydantic import BaseModel, Field
from typing import List, Dict, Any, Optional
from datetime import datetime


class AgentMetadata(BaseModel):
    """Agent metadata."""
    
    downloads: int = Field(0, description="Total download count")
    stars: int = Field(0, description="Star count")
    rating: Optional[float] = Field(None, description="Average rating (1-5)")
    rating_count: int = Field(0, description="Number of ratings")
    
    # Categories and tags
    categories: List[str] = Field(default_factory=list, description="Categories")
    tags: List[str] = Field(default_factory=list, description="Tags")
    
    # Verification
    verified: bool = Field(False, description="Whether agent is verified")
    official: bool = Field(False, description="Whether agent is official")


class AgentVersion(BaseModel):
    """Agent version entity."""
    
    id: Optional[str] = Field(None, description="Version ID")
    agent_id: Optional[str] = Field(None, description="Parent agent ID")
    version: str = Field(..., description="Version string")
    tag: Optional[str] = Field(None, description="Version tag (e.g., 'latest')")
    
    # Manifest
    manifest: Dict[str, Any] = Field(default_factory=dict, description="Agent manifest")
    
    # File info
    file_path: Optional[str] = Field(None, description="Storage path")
    file_size: Optional[int] = Field(None, description="File size in bytes")
    checksum: Optional[str] = Field(None, description="SHA256 checksum")
    
    # Timestamps
    created_at: Optional[datetime] = Field(None, description="Creation timestamp")
    
    # Status
    deprecated: bool = Field(False, description="Whether version is deprecated")
    deprecation_message: Optional[str] = Field(None, description="Deprecation message")


class Agent(BaseModel):
    """Agent entity."""
    
    id: Optional[str] = Field(None, description="Agent ID")
    name: str = Field(..., description="Agent name")
    namespace: Optional[str] = Field(None, description="Namespace (user/org)")
    full_name: Optional[str] = Field(None, description="Full name (namespace/name)")
    
    # Basic info
    description: Optional[str] = Field(None, description="Agent description")
    framework: str = Field(..., description="Framework name")
    
    # Owner
    owner_id: Optional[str] = Field(None, description="Owner user ID")
    owner_name: Optional[str] = Field(None, description="Owner username")
    organization_id: Optional[str] = Field(None, description="Organization ID")
    
    # Visibility
    is_public: bool = Field(True, description="Whether agent is public")
    
    # Versions
    latest_version: Optional[str] = Field(None, description="Latest version string")
    versions: List[AgentVersion] = Field(default_factory=list, description="Version list")
    
    # Metadata
    metadata: AgentMetadata = Field(default_factory=AgentMetadata, description="Agent metadata")
    
    # Timestamps
    created_at: Optional[datetime] = Field(None, description="Creation timestamp")
    updated_at: Optional[datetime] = Field(None, description="Last update timestamp")


class AgentSummary(BaseModel):
    """Agent summary for list views."""
    
    name: str = Field(..., description="Agent name")
    full_name: Optional[str] = Field(None, description="Full name (namespace/name)")
    description: Optional[str] = Field(None, description="Agent description")
    framework: str = Field(..., description="Framework name")
    latest_version: Optional[str] = Field(None, description="Latest version")
    
    # Quick stats
    downloads: int = Field(0, description="Download count")
    stars: int = Field(0, description="Star count")
    
    # Flags
    verified: bool = Field(False, description="Verified")
    official: bool = Field(False, description="Official")
    
    # Timestamps
    updated_at: Optional[datetime] = Field(None, description="Last update")
