"""ServiceItems Resource."""

from typing import Any, Dict, Generator, List, Union

from netorca_sdk.v2.base import BaseResource
from netorca_sdk.v2.models.service_item import ServiceItem
from netorca_sdk.v2.types import ServiceItemFilters, ServiceItemRuntimeState


class ServiceItemsResource(BaseResource):
    """Service Items resource."""

    endpoint = "service_items"
    model_class = ServiceItem

    SUPPORTED_FILTERS = {
        "application_id": int,
        "application_name": str,
        "application_name_contains": str,
        "change_state": str,
        "consumer_team_id": int,
        "name": str,
        "ordering": str,
        "runtime_state": str,
        "service_id": int,
        "service_name": str,
        "service_owner_id": int,
        "service_owner_team_id": int,
    }

    def list(self, exclude_decommissioned: bool = True, **filters: Any) -> Generator[ServiceItem, None, None]:
        """
        List service items with optional filters.

        Args:
            exclude_decommissioned: Auto-exclude decommissioned items (default: True)
            **filters: Additional filter parameters (see ServiceItemFilters)
                - application_id: int
                - application_name: str
                - change_state: str | ServiceItemChangeState
                - runtime_state: str | ServiceItemRuntimeState
                - service_id: int
                - ordering: str (e.g., "name", "-created")
                - etc. See ServiceItemFilters for full list

        Yields:
            ServiceItem instances
        """
        # Auto-exclude decommissioned unless explicitly requesting them
        if exclude_decommissioned and 'runtime_state' not in filters:
            filters['runtime_state'] = f'{ServiceItem.RUNTIME_STATE_REQUESTED},{ServiceItem.RUNTIME_STATE_IN_SERVICE}'

        return super().list(**filters)

    def by_service(self, service_id: int, **filters: Any) -> List[ServiceItem]:
        """
        Get all service items for a service.

        Args:
            service_id: Service ID
            **filters: Additional filters

        Returns:
            List of ServiceItem instances
        """
        return self.filter(service_id=service_id, **filters)

    def by_application(self, application_id: int, **filters: Any) -> List[ServiceItem]:
        """
        Get all service items for an application.

        Args:
            application_id: Application ID
            **filters: Additional filters

        Returns:
            List of ServiceItem instances
        """
        return self.filter(application_id=application_id, **filters)

    def in_service(self, **filters: Any) -> List[ServiceItem]:
        """
        Get all items in IN_SERVICE state.

        Args:
            **filters: Additional filters

        Returns:
            List of ServiceItem instances
        """
        return self.filter(runtime_state=ServiceItem.RUNTIME_STATE_IN_SERVICE, **filters)

    def requested(self, **filters: Any) -> List[ServiceItem]:
        """
        Get all items in REQUESTED state.

        Args:
            **filters: Additional filters

        Returns:
            List of ServiceItem instances
        """
        return self.filter(runtime_state=ServiceItem.RUNTIME_STATE_REQUESTED, **filters)

    def get_dependant(self, **filters: Any) -> List[Dict[str, Any]]:
        """
        Get dependent service items.

        Returns:
            Raw response data (structure varies)
        """
        # This uses a custom path
        results = []
        for item in self.client.get(self.endpoint, params=filters, custom_path="service_items/dependant"):
            results.append(item)
        return results
