"""ServiceItem Model."""

from typing import Optional

from netorca_sdk.v2.base import BaseModel
from netorca_sdk.v2.types import ServiceItemChangeState, ServiceItemRuntimeState


class ServiceItem(BaseModel):
    """
    Service Item model.

    Service items are configuration items managed by NetOrca with
    runtime states and change tracking.
    """

    # Runtime state constants (for backwards compatibility)
    RUNTIME_STATE_REQUESTED = ServiceItemRuntimeState.REQUESTED.value
    RUNTIME_STATE_IN_SERVICE = ServiceItemRuntimeState.IN_SERVICE.value
    RUNTIME_STATE_DECOMMISSIONED = ServiceItemRuntimeState.DECOMMISSIONED.value

    @property
    def name(self) -> Optional[str]:
        """Get service item name."""
        return self.data.get('name')

    @property
    def runtime_state(self) -> Optional[str]:
        """Get current runtime state."""
        return self.data.get('runtime_state')

    @property
    def change_state(self) -> Optional[str]:
        """Get current change state."""
        return self.data.get('change_state')

    @property
    def service_id(self) -> Optional[int]:
        """Get associated service ID."""
        return self._extract_nested_id(self.data.get('service'))

    @property
    def service_name(self) -> Optional[str]:
        """Get associated service name."""
        return self._extract_nested_field(self.data.get('service'), 'name')

    @property
    def application_id(self) -> Optional[int]:
        """Get associated application ID."""
        return self._extract_nested_id(self.data.get('application'))

    @property
    def application_name(self) -> Optional[str]:
        """Get associated application name."""
        return self._extract_nested_field(self.data.get('application'), 'name')

    @property
    def service_owner_team_id(self) -> Optional[int]:
        """Get service owner team ID."""
        return self._extract_nested_id(self.data.get('service_owner_team'))

    @property
    def service_owner_team_name(self) -> Optional[str]:
        """Get service owner team name."""
        return self._extract_nested_field(self.data.get('service_owner_team'), 'name')

    @property
    def consumer_team_id(self) -> Optional[int]:
        """Get consumer team ID."""
        return self._extract_nested_id(self.data.get('consumer_team'))

    @property
    def consumer_team_name(self) -> Optional[str]:
        """Get consumer team name."""
        return self._extract_nested_field(self.data.get('consumer_team'), 'name')

    @property
    def related(self) -> Optional[str]:
        """Get related URL."""
        return self.data.get('related')

    def is_requested(self) -> bool:
        """Check if item is in REQUESTED state."""
        return self.runtime_state == self.RUNTIME_STATE_REQUESTED

    def is_in_service(self) -> bool:
        """Check if item is in IN_SERVICE state."""
        return self.runtime_state == self.RUNTIME_STATE_IN_SERVICE

    def is_decommissioned(self) -> bool:
        """Check if item is in DECOMMISSIONED state."""
        return self.runtime_state == self.RUNTIME_STATE_DECOMMISSIONED

    def __repr__(self) -> str:
        """String representation."""
        return f"ServiceItem(id={self.id}, name={self.name!r}, runtime_state={self.runtime_state!r})"
