"""Audio source schemas (CRUD)."""

from datetime import datetime
from typing import List, Literal, Optional

from pydantic import BaseModel, Field


class AudioSourceCreate(BaseModel):
    """Request to create an audio source."""

    name: str = Field(description="Source name", min_length=1, max_length=100)
    source_type: Literal["multichannel", "mono", "band_extract"] = Field(description="Source type")
    # multichannel fields
    device_index: Optional[int] = Field(None, description="Audio device index (-1 = default)")
    is_loopback: Optional[bool] = Field(None, description="True for system audio (WASAPI loopback)")
    audio_template_id: Optional[str] = Field(None, description="Audio capture template ID")
    # mono fields
    audio_source_id: Optional[str] = Field(None, description="Parent audio source ID")
    channel: Optional[str] = Field(None, description="Channel: mono|left|right")
    # band_extract fields
    band: Optional[str] = Field(None, description="Band preset: bass|mid|treble|custom")
    freq_low: Optional[float] = Field(None, description="Low frequency bound (Hz)", ge=20, le=20000)
    freq_high: Optional[float] = Field(None, description="High frequency bound (Hz)", ge=20, le=20000)
    description: Optional[str] = Field(None, description="Optional description", max_length=500)
    tags: List[str] = Field(default_factory=list, description="User-defined tags")


class AudioSourceUpdate(BaseModel):
    """Request to update an audio source."""

    name: Optional[str] = Field(None, description="Source name", min_length=1, max_length=100)
    device_index: Optional[int] = Field(None, description="Audio device index (-1 = default)")
    is_loopback: Optional[bool] = Field(None, description="True for system audio (WASAPI loopback)")
    audio_template_id: Optional[str] = Field(None, description="Audio capture template ID")
    audio_source_id: Optional[str] = Field(None, description="Parent audio source ID")
    channel: Optional[str] = Field(None, description="Channel: mono|left|right")
    band: Optional[str] = Field(None, description="Band preset: bass|mid|treble|custom")
    freq_low: Optional[float] = Field(None, description="Low frequency bound (Hz)", ge=20, le=20000)
    freq_high: Optional[float] = Field(None, description="High frequency bound (Hz)", ge=20, le=20000)
    description: Optional[str] = Field(None, description="Optional description", max_length=500)
    tags: Optional[List[str]] = None


class AudioSourceResponse(BaseModel):
    """Audio source response."""

    id: str = Field(description="Source ID")
    name: str = Field(description="Source name")
    source_type: str = Field(description="Source type: multichannel, mono, or band_extract")
    device_index: Optional[int] = Field(None, description="Audio device index")
    is_loopback: Optional[bool] = Field(None, description="WASAPI loopback mode")
    audio_template_id: Optional[str] = Field(None, description="Audio capture template ID")
    audio_source_id: Optional[str] = Field(None, description="Parent audio source ID")
    channel: Optional[str] = Field(None, description="Channel: mono|left|right")
    band: Optional[str] = Field(None, description="Band preset: bass|mid|treble|custom")
    freq_low: Optional[float] = Field(None, description="Low frequency bound (Hz)")
    freq_high: Optional[float] = Field(None, description="High frequency bound (Hz)")
    description: Optional[str] = Field(None, description="Description")
    tags: List[str] = Field(default_factory=list, description="User-defined tags")
    created_at: datetime = Field(description="Creation timestamp")
    updated_at: datetime = Field(description="Last update timestamp")


class AudioSourceListResponse(BaseModel):
    """List of audio sources."""

    sources: List[AudioSourceResponse] = Field(description="List of audio sources")
    count: int = Field(description="Number of sources")
