"""Home Assistant source storage using SQLite."""

import uuid
from datetime import datetime, timezone
from typing import List, Optional

from wled_controller.storage.base_sqlite_store import BaseSqliteStore
from wled_controller.storage.database import Database
from wled_controller.storage.home_assistant_source import HomeAssistantSource
from wled_controller.utils import get_logger

logger = get_logger(__name__)


class HomeAssistantStore(BaseSqliteStore[HomeAssistantSource]):
    """Persistent storage for Home Assistant sources."""

    _table_name = "home_assistant_sources"
    _entity_name = "Home Assistant source"

    def __init__(self, db: Database):
        super().__init__(db, HomeAssistantSource.from_dict)

    # Backward-compatible aliases
    get_all_sources = BaseSqliteStore.get_all
    get_source = BaseSqliteStore.get
    delete_source = BaseSqliteStore.delete

    def create_source(
        self,
        name: str,
        host: str,
        token: str,
        use_ssl: bool = False,
        entity_filters: Optional[List[str]] = None,
        description: Optional[str] = None,
        tags: Optional[List[str]] = None,
    ) -> HomeAssistantSource:
        if not host:
            raise ValueError("host is required")
        if not token:
            raise ValueError("token is required")

        self._check_name_unique(name)

        sid = f"has_{uuid.uuid4().hex[:8]}"
        now = datetime.now(timezone.utc)

        source = HomeAssistantSource(
            id=sid,
            name=name,
            created_at=now,
            updated_at=now,
            host=host,
            token=token,
            use_ssl=use_ssl,
            entity_filters=entity_filters or [],
            description=description,
            tags=tags or [],
        )

        self._items[sid] = source
        self._save_item(sid, source)
        logger.info(f"Created Home Assistant source: {name} ({sid})")
        return source

    def update_source(
        self,
        source_id: str,
        name: Optional[str] = None,
        host: Optional[str] = None,
        token: Optional[str] = None,
        use_ssl: Optional[bool] = None,
        entity_filters: Optional[List[str]] = None,
        description: Optional[str] = None,
        tags: Optional[List[str]] = None,
    ) -> HomeAssistantSource:
        existing = self.get(source_id)

        if name is not None and name != existing.name:
            self._check_name_unique(name)

        updated = HomeAssistantSource(
            id=existing.id,
            name=name if name is not None else existing.name,
            created_at=existing.created_at,
            updated_at=datetime.now(timezone.utc),
            host=host if host is not None else existing.host,
            token=token if token is not None else existing.token,
            use_ssl=use_ssl if use_ssl is not None else existing.use_ssl,
            entity_filters=(
                entity_filters if entity_filters is not None else existing.entity_filters
            ),
            description=description if description is not None else existing.description,
            tags=tags if tags is not None else existing.tags,
        )

        self._items[source_id] = updated
        self._save_item(source_id, updated)
        logger.info(f"Updated Home Assistant source: {updated.name} ({source_id})")
        return updated
