"""Home Assistant source data model.

A HomeAssistantSource represents a connection to a Home Assistant instance.
It stores the host, access token, and optional entity filters.
Referenced by HomeAssistantColorStripSource and HomeAssistantCondition.
"""

from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import List, Optional


def _parse_common(data: dict) -> dict:
    """Extract common fields from a dict, parsing timestamps."""
    created = data.get("created_at", "")
    updated = data.get("updated_at", "")
    return {
        "id": data["id"],
        "name": data["name"],
        "created_at": (
            datetime.fromisoformat(created)
            if isinstance(created, str) and created
            else datetime.now(timezone.utc)
        ),
        "updated_at": (
            datetime.fromisoformat(updated)
            if isinstance(updated, str) and updated
            else datetime.now(timezone.utc)
        ),
        "description": data.get("description"),
        "tags": data.get("tags") or [],
    }


@dataclass
class HomeAssistantSource:
    """Home Assistant connection configuration."""

    id: str
    name: str
    created_at: datetime
    updated_at: datetime
    host: str = ""  # e.g. "192.168.1.100:8123"
    token: str = ""  # Long-Lived Access Token
    use_ssl: bool = False
    entity_filters: List[str] = field(
        default_factory=list
    )  # optional allowlist (e.g. ["sensor.*"])
    description: Optional[str] = None
    tags: List[str] = field(default_factory=list)

    @property
    def ws_url(self) -> str:
        """Build WebSocket URL from host and SSL setting."""
        scheme = "wss" if self.use_ssl else "ws"
        return f"{scheme}://{self.host}/api/websocket"

    @property
    def http_url(self) -> str:
        """Build HTTP URL from host and SSL setting."""
        scheme = "https" if self.use_ssl else "http"
        return f"{scheme}://{self.host}"

    def to_dict(self) -> dict:
        return {
            "id": self.id,
            "name": self.name,
            "host": self.host,
            "token": self.token,
            "use_ssl": self.use_ssl,
            "entity_filters": list(self.entity_filters),
            "description": self.description,
            "tags": self.tags,
            "created_at": self.created_at.isoformat(),
            "updated_at": self.updated_at.isoformat(),
        }

    @staticmethod
    def from_dict(data: dict) -> "HomeAssistantSource":
        common = _parse_common(data)
        return HomeAssistantSource(
            **common,
            host=data.get("host", ""),
            token=data.get("token", ""),
            use_ssl=data.get("use_ssl", False),
            entity_filters=data.get("entity_filters") or [],
        )
