"""Data models for Heiman Connect library."""

from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Optional


@dataclass
class HeimanUser:
    """Heiman user model."""

    user_id: str
    email: str
    nickname: Optional[str] = None
    phone: Optional[str] = None
    avatar: Optional[str] = None
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None
    raw_data: dict[str, Any] = field(default_factory=dict)


@dataclass
class HeimanHome:
    """Heiman home model."""

    home_id: str
    home_name: str
    user_id: str
    room_count: int = 0
    device_count: int = 0
    cover_url: Optional[str] = None
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None
    raw_data: dict[str, Any] = field(default_factory=dict)


@dataclass
class DeviceProperty:
    """Device property model."""

    identifier: str
    name: str
    value: Any = None
    unit: Optional[str] = None
    data_type: Optional[str] = None
    readable: bool = True
    writable: bool = False
    description: Optional[str] = None
    entity: Optional[str] = None


@dataclass
class HeimanDevice:
    """Heiman device model."""

    device_id: str
    device_name: str
    product_id: str
    home_id: str
    room_id: Optional[str] = None
    device_type: Optional[str] = None
    model: Optional[str] = None
    manufacturer: str = "Heiman"
    firmware_version: Optional[str] = None
    hardware_version: Optional[str] = None
    serial_number: Optional[str] = None
    mac_address: Optional[str] = None
    ip_address: Optional[str] = None
    online: bool = False
    properties: dict[str, DeviceProperty] = field(default_factory=dict)
    capabilities: list[str] = field(default_factory=list)
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None
    raw_data: dict[str, Any] = field(default_factory=dict)

    @property
    def device_info(self) -> dict[str, Any]:
        """Get device information for Home Assistant device registry."""
        return {
            "identifiers": {(DOMAIN, self.device_id)},
            "name": self.device_name,
            "manufacturer": self.manufacturer,
            "model": self.model or self.product_id,
            "sw_version": self.firmware_version,
            "hw_version": self.hardware_version,
            "serial_number": self.serial_number,
        }

    def update_property(self, identifier: str, value: Any) -> None:
        """Update device property value.
        
        Args:
            identifier: Property identifier
            value: New property value
        """
        if identifier in self.properties:
            self.properties[identifier].value = value
        else:
            self.properties[identifier] = DeviceProperty(
                identifier=identifier,
                name=identifier,
                value=value,
            )
        self.updated_at = datetime.now()


# Import DOMAIN from const to avoid circular import
# This will be set by the main integration
DOMAIN = "heiman_home"
