"""
Skill entity models.
"""

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


class SkillMetadataInfo(BaseModel):
    """Skill metadata (hub stats)."""

    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 skill is verified")


class SkillVersion(BaseModel):
    """Skill version entity."""

    id: Optional[str] = Field(None, description="Version ID")
    skill_id: Optional[str] = Field(None, description="Parent skill 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="Skill 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")


class Skill(BaseModel):
    """Skill entity."""

    id: Optional[str] = Field(None, description="Skill ID")
    name: str = Field(..., description="Skill 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="Skill description")

    # 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 skill is public")

    # Versions
    latest_version: Optional[str] = Field(None, description="Latest version string")
    versions: List[SkillVersion] = Field(default_factory=list, description="Version list")

    # Metadata
    metadata: SkillMetadataInfo = Field(
        default_factory=SkillMetadataInfo, description="Skill metadata"
    )

    # Skill-specific summary info
    skill_count: int = Field(0, description="Number of skills in pack")
    platforms: List[str] = Field(default_factory=list, description="Compatible platforms")

    # Timestamps
    created_at: Optional[datetime] = Field(None, description="Creation timestamp")
    updated_at: Optional[datetime] = Field(None, description="Last update timestamp")


class SkillSummary(BaseModel):
    """Skill summary for list views."""

    name: str = Field(..., description="Skill name")
    full_name: Optional[str] = Field(None, description="Full name (namespace/name)")
    description: Optional[str] = Field(None, description="Skill description")
    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")

    # Skill-specific
    skill_count: int = Field(0, description="Number of skills in pack")
    platforms: List[str] = Field(default_factory=list, description="Compatible platforms")

    # Flags
    verified: bool = Field(False, description="Verified")

    # Timestamps
    updated_at: Optional[datetime] = Field(None, description="Last update")
