"""
Pydantic schemas for Service model.

Request and response schemas for services running on infrastructure.
"""

from datetime import datetime
from typing import Optional
from uuid import UUID

from pydantic import BaseModel, Field


class ServiceBase(BaseModel):
    """Base schema with shared Service fields."""

    infrastructure_id: Optional[str] = Field(None, description="Foreign key to infrastructure table (UUID)")
    service_name: str = Field(..., description="Name of the service (e.g., 'Gitea', 'PostgreSQL', 'Apache')")
    service_type: Optional[str] = Field(None, description="Type of service (e.g., 'git_hosting', 'database', 'web_server')")
    external_url: Optional[str] = Field(None, description="External URL for accessing the service")
    internal_url: Optional[str] = Field(None, description="Internal URL for accessing the service")
    port: Optional[int] = Field(None, description="Port number the service runs on")
    protocol: Optional[str] = Field(None, description="Protocol used (https, ssh, smb, etc.)")
    status: str = Field("running", description="Status: running, stopped, error, maintenance")
    version: Optional[str] = Field(None, description="Version of the service")
    notes: Optional[str] = Field(None, description="Additional notes")


class ServiceCreate(ServiceBase):
    """Schema for creating a new Service."""
    pass


class ServiceUpdate(BaseModel):
    """Schema for updating an existing Service. All fields are optional."""

    infrastructure_id: Optional[str] = Field(None, description="Foreign key to infrastructure table (UUID)")
    service_name: Optional[str] = Field(None, description="Name of the service (e.g., 'Gitea', 'PostgreSQL', 'Apache')")
    service_type: Optional[str] = Field(None, description="Type of service (e.g., 'git_hosting', 'database', 'web_server')")
    external_url: Optional[str] = Field(None, description="External URL for accessing the service")
    internal_url: Optional[str] = Field(None, description="Internal URL for accessing the service")
    port: Optional[int] = Field(None, description="Port number the service runs on")
    protocol: Optional[str] = Field(None, description="Protocol used (https, ssh, smb, etc.)")
    status: Optional[str] = Field(None, description="Status: running, stopped, error, maintenance")
    version: Optional[str] = Field(None, description="Version of the service")
    notes: Optional[str] = Field(None, description="Additional notes")


class ServiceResponse(ServiceBase):
    """Schema for Service responses with ID and timestamps."""

    id: UUID = Field(..., description="Unique identifier for the service")
    created_at: datetime = Field(..., description="Timestamp when the service was created")
    updated_at: datetime = Field(..., description="Timestamp when the service was last updated")

    model_config = {"from_attributes": True}
