# app/schemas/syndarix/agent_instance.py
"""
Pydantic schemas for AgentInstance entity.
"""

from datetime import datetime
from decimal import Decimal
from typing import Any
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field

from .enums import AgentStatus


class AgentInstanceBase(BaseModel):
    """Base agent instance schema with common fields."""

    agent_type_id: UUID
    project_id: UUID
    status: AgentStatus = AgentStatus.IDLE
    current_task: str | None = None
    short_term_memory: dict[str, Any] = Field(default_factory=dict)
    long_term_memory_ref: str | None = Field(None, max_length=500)
    session_id: str | None = Field(None, max_length=255)


class AgentInstanceCreate(BaseModel):
    """Schema for creating a new agent instance."""

    agent_type_id: UUID
    project_id: UUID
    name: str = Field(..., min_length=1, max_length=100)
    status: AgentStatus = AgentStatus.IDLE
    current_task: str | None = None
    short_term_memory: dict[str, Any] = Field(default_factory=dict)
    long_term_memory_ref: str | None = Field(None, max_length=500)
    session_id: str | None = Field(None, max_length=255)


class AgentInstanceUpdate(BaseModel):
    """Schema for updating an agent instance."""

    status: AgentStatus | None = None
    current_task: str | None = None
    short_term_memory: dict[str, Any] | None = None
    long_term_memory_ref: str | None = None
    session_id: str | None = None
    last_activity_at: datetime | None = None
    tasks_completed: int | None = Field(None, ge=0)
    tokens_used: int | None = Field(None, ge=0)
    cost_incurred: Decimal | None = Field(None, ge=0)


class AgentInstanceTerminate(BaseModel):
    """Schema for terminating an agent instance."""

    reason: str | None = None


class AgentInstanceInDB(AgentInstanceBase):
    """Schema for agent instance in database."""

    id: UUID
    last_activity_at: datetime | None = None
    terminated_at: datetime | None = None
    tasks_completed: int = 0
    tokens_used: int = 0
    cost_incurred: Decimal = Decimal("0.0000")
    created_at: datetime
    updated_at: datetime

    model_config = ConfigDict(from_attributes=True)


class AgentInstanceResponse(BaseModel):
    """Schema for agent instance API responses."""

    id: UUID
    agent_type_id: UUID
    project_id: UUID
    name: str
    status: AgentStatus
    current_task: str | None = None
    short_term_memory: dict[str, Any] = Field(default_factory=dict)
    long_term_memory_ref: str | None = None
    session_id: str | None = None
    last_activity_at: datetime | None = None
    terminated_at: datetime | None = None
    tasks_completed: int = 0
    tokens_used: int = 0
    cost_incurred: Decimal = Decimal("0.0000")
    created_at: datetime
    updated_at: datetime

    # Expanded fields from relationships
    agent_type_name: str | None = None
    agent_type_slug: str | None = None
    project_name: str | None = None
    project_slug: str | None = None
    assigned_issues_count: int | None = 0

    model_config = ConfigDict(from_attributes=True)


class AgentInstanceListResponse(BaseModel):
    """Schema for paginated agent instance list responses."""

    agent_instances: list[AgentInstanceResponse]
    total: int
    page: int
    page_size: int
    pages: int


class AgentInstanceMetrics(BaseModel):
    """Schema for agent instance metrics summary."""

    total_instances: int
    active_instances: int
    idle_instances: int
    total_tasks_completed: int
    total_tokens_used: int
    total_cost_incurred: Decimal
