"""MQTT source schemas (CRUD + test + status)."""

from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, Field


class MQTTSourceCreate(BaseModel):
    """Request to create an MQTT source."""

    name: str = Field(description="Source name", min_length=1, max_length=100)
    broker_host: str = Field(description="MQTT broker hostname or IP", min_length=1)
    broker_port: int = Field(default=1883, description="MQTT broker port", ge=1, le=65535)
    username: str = Field(default="", description="Broker username (optional)")
    password: str = Field(default="", description="Broker password (optional)")
    client_id: str = Field(default="ledgrab", description="MQTT client ID")
    base_topic: str = Field(default="ledgrab", description="Base topic prefix")
    description: Optional[str] = Field(None, description="Optional description", max_length=500)
    tags: List[str] = Field(default_factory=list, description="User-defined tags")


class MQTTSourceUpdate(BaseModel):
    """Request to update an MQTT source."""

    name: Optional[str] = Field(None, description="Source name", min_length=1, max_length=100)
    broker_host: Optional[str] = Field(None, description="MQTT broker hostname or IP", min_length=1)
    broker_port: Optional[int] = Field(None, description="MQTT broker port", ge=1, le=65535)
    username: Optional[str] = Field(None, description="Broker username")
    password: Optional[str] = Field(None, description="Broker password")
    client_id: Optional[str] = Field(None, description="MQTT client ID")
    base_topic: Optional[str] = Field(None, description="Base topic prefix")
    description: Optional[str] = Field(None, description="Optional description", max_length=500)
    tags: Optional[List[str]] = None


class MQTTSourceResponse(BaseModel):
    """MQTT source response."""

    id: str = Field(description="Source ID")
    name: str = Field(description="Source name")
    broker_host: str = Field(description="Broker hostname or IP")
    broker_port: int = Field(description="Broker port")
    username: str = Field(default="", description="Broker username")
    password_set: bool = Field(default=False, description="Whether a password is configured")
    client_id: str = Field(description="MQTT client ID")
    base_topic: str = Field(description="Base topic prefix")
    connected: bool = Field(default=False, description="Whether the broker connection is active")
    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 MQTTSourceListResponse(BaseModel):
    """List of MQTT sources."""

    sources: List[MQTTSourceResponse] = Field(description="List of MQTT sources")
    count: int = Field(description="Number of sources")


class MQTTTestResponse(BaseModel):
    """Connection test result."""

    success: bool = Field(description="Whether broker connection succeeded")
    error: Optional[str] = Field(None, description="Error message if connection failed")


class MQTTConnectionStatus(BaseModel):
    """Connection status for dashboard indicators."""

    source_id: str
    name: str
    connected: bool
    broker: str


class MQTTStatusResponse(BaseModel):
    """Overall MQTT integration status for dashboard."""

    connections: List[MQTTConnectionStatus]
    total_sources: int
    connected_count: int
