"""
Layer 1: Primitives - Opinionated helper methods for spicedb
These helpers provide higher-level abstractions over the raw API client.
Each method wraps a Layer 0 operation with the _structured suffix.
"""
from typing import Any, Dict, List, Literal, Optional, Type, TypeVar, Union
from pydantic import BaseModel
from .raw import *

T = TypeVar('T', bound=BaseModel)


class SpicedbPrimitives:
    """
    Opinionated helper methods for spicedb.

    Generated Layer 1 primitives for 8 operations.
    """

    def __init__(self, permissions_client: PermissionsClient):
        self.permissions_client = permissions_client

    async def permissions_service_check_permission_structured(
        self,
        body: Dict[str, Any],
        auth_token: Optional[str] = None,
        response_model: Optional[Type[T]] = None
    ) -> T | Dict[str, Any]:
        """
        Structured wrapper for permissions_service_check_permission
        This is a Layer 1 primitive that wraps the Layer 0 permissions_service_check_permission() method.
        If response_model is provided, the response will be parsed into that Pydantic model.

        Args:
            auth_token: JWT token for authentication
            body: Request body
            response_model: Optional Pydantic model to parse the response into
        Returns:
            Parsed response as the specified Pydantic model, or raw Dict if no model provided
        """
        # Call Layer 0 method
        response = await self.permissions_client.permissions_service_check_permission(
            body=body,
            auth_token=auth_token
        )

        # If no response model specified, return raw response
        if response_model is None:
            return response

        # Parse response into Pydantic model
        try:
            return response_model.model_validate(response)
        except Exception as e:
            raise ValueError(f"Failed to parse response as {response_model.__name__}: {e}")

    async def permissions_service_check_bulk_permissions_structured(
        self,
        body: Dict[str, Any],
        auth_token: Optional[str] = None,
        response_model: Optional[Type[T]] = None
    ) -> T | Dict[str, Any]:
        """
        Structured wrapper for permissions_service_check_bulk_permissions
        This is a Layer 1 primitive that wraps the Layer 0 permissions_service_check_bulk_permissions() method.
        If response_model is provided, the response will be parsed into that Pydantic model.

        Args:
            auth_token: JWT token for authentication
            body: Request body
            response_model: Optional Pydantic model to parse the response into
        Returns:
            Parsed response as the specified Pydantic model, or raw Dict if no model provided
        """
        # Call Layer 0 method
        response = await self.permissions_client.permissions_service_check_bulk_permissions(
            body=body,
            auth_token=auth_token
        )

        # If no response model specified, return raw response
        if response_model is None:
            return response

        # Parse response into Pydantic model
        try:
            return response_model.model_validate(response)
        except Exception as e:
            raise ValueError(f"Failed to parse response as {response_model.__name__}: {e}")

    async def permissions_service_expand_permission_tree_structured(
        self,
        body: Dict[str, Any],
        auth_token: Optional[str] = None,
        response_model: Optional[Type[T]] = None
    ) -> T | Dict[str, Any]:
        """
        Structured wrapper for permissions_service_expand_permission_tree
        This is a Layer 1 primitive that wraps the Layer 0 permissions_service_expand_permission_tree() method.
        If response_model is provided, the response will be parsed into that Pydantic model.

        Args:
            auth_token: JWT token for authentication
            body: Request body
            response_model: Optional Pydantic model to parse the response into
        Returns:
            Parsed response as the specified Pydantic model, or raw Dict if no model provided
        """
        # Call Layer 0 method
        response = await self.permissions_client.permissions_service_expand_permission_tree(
            body=body,
            auth_token=auth_token
        )

        # If no response model specified, return raw response
        if response_model is None:
            return response

        # Parse response into Pydantic model
        try:
            return response_model.model_validate(response)
        except Exception as e:
            raise ValueError(f"Failed to parse response as {response_model.__name__}: {e}")

    async def permissions_service_lookup_resources_structured(
        self,
        body: Dict[str, Any],
        auth_token: Optional[str] = None,
        response_model: Optional[Type[T]] = None
    ) -> T | Dict[str, Any]:
        """
        Structured wrapper for permissions_service_lookup_resources
        This is a Layer 1 primitive that wraps the Layer 0 permissions_service_lookup_resources() method.
        If response_model is provided, the response will be parsed into that Pydantic model.

        Args:
            auth_token: JWT token for authentication
            body: Request body
            response_model: Optional Pydantic model to parse the response into
        Returns:
            Parsed response as the specified Pydantic model, or raw Dict if no model provided
        """
        # Call Layer 0 method
        response = await self.permissions_client.permissions_service_lookup_resources(
            body=body,
            auth_token=auth_token
        )

        # If no response model specified, return raw response
        if response_model is None:
            return response

        # Parse response into Pydantic model
        try:
            return response_model.model_validate(response)
        except Exception as e:
            raise ValueError(f"Failed to parse response as {response_model.__name__}: {e}")

    async def permissions_service_lookup_subjects_structured(
        self,
        body: Dict[str, Any],
        auth_token: Optional[str] = None,
        response_model: Optional[Type[T]] = None
    ) -> T | Dict[str, Any]:
        """
        Structured wrapper for permissions_service_lookup_subjects
        This is a Layer 1 primitive that wraps the Layer 0 permissions_service_lookup_subjects() method.
        If response_model is provided, the response will be parsed into that Pydantic model.

        Args:
            auth_token: JWT token for authentication
            body: Request body
            response_model: Optional Pydantic model to parse the response into
        Returns:
            Parsed response as the specified Pydantic model, or raw Dict if no model provided
        """
        # Call Layer 0 method
        response = await self.permissions_client.permissions_service_lookup_subjects(
            body=body,
            auth_token=auth_token
        )

        # If no response model specified, return raw response
        if response_model is None:
            return response

        # Parse response into Pydantic model
        try:
            return response_model.model_validate(response)
        except Exception as e:
            raise ValueError(f"Failed to parse response as {response_model.__name__}: {e}")

    async def permissions_service_delete_relationships_structured(
        self,
        body: Dict[str, Any],
        auth_token: Optional[str] = None,
        response_model: Optional[Type[T]] = None
    ) -> T | Dict[str, Any]:
        """
        Structured wrapper for permissions_service_delete_relationships
        This is a Layer 1 primitive that wraps the Layer 0 permissions_service_delete_relationships() method.
        If response_model is provided, the response will be parsed into that Pydantic model.

        Args:
            auth_token: JWT token for authentication
            body: Request body
            response_model: Optional Pydantic model to parse the response into
        Returns:
            Parsed response as the specified Pydantic model, or raw Dict if no model provided
        """
        # Call Layer 0 method
        response = await self.permissions_client.permissions_service_delete_relationships(
            body=body,
            auth_token=auth_token
        )

        # If no response model specified, return raw response
        if response_model is None:
            return response

        # Parse response into Pydantic model
        try:
            return response_model.model_validate(response)
        except Exception as e:
            raise ValueError(f"Failed to parse response as {response_model.__name__}: {e}")

    async def permissions_service_read_relationships_structured(
        self,
        body: Dict[str, Any],
        auth_token: Optional[str] = None,
        response_model: Optional[Type[T]] = None
    ) -> T | Dict[str, Any]:
        """
        Structured wrapper for permissions_service_read_relationships
        This is a Layer 1 primitive that wraps the Layer 0 permissions_service_read_relationships() method.
        If response_model is provided, the response will be parsed into that Pydantic model.

        Args:
            auth_token: JWT token for authentication
            body: Request body
            response_model: Optional Pydantic model to parse the response into
        Returns:
            Parsed response as the specified Pydantic model, or raw Dict if no model provided
        """
        # Call Layer 0 method
        response = await self.permissions_client.permissions_service_read_relationships(
            body=body,
            auth_token=auth_token
        )

        # If no response model specified, return raw response
        if response_model is None:
            return response

        # Parse response into Pydantic model
        try:
            return response_model.model_validate(response)
        except Exception as e:
            raise ValueError(f"Failed to parse response as {response_model.__name__}: {e}")

    async def permissions_service_write_relationships_structured(
        self,
        body: Dict[str, Any],
        auth_token: Optional[str] = None,
        response_model: Optional[Type[T]] = None
    ) -> T | Dict[str, Any]:
        """
        Structured wrapper for permissions_service_write_relationships
        This is a Layer 1 primitive that wraps the Layer 0 permissions_service_write_relationships() method.
        If response_model is provided, the response will be parsed into that Pydantic model.

        Args:
            auth_token: JWT token for authentication
            body: Request body
            response_model: Optional Pydantic model to parse the response into
        Returns:
            Parsed response as the specified Pydantic model, or raw Dict if no model provided
        """
        # Call Layer 0 method
        response = await self.permissions_client.permissions_service_write_relationships(
            body=body,
            auth_token=auth_token
        )

        # If no response model specified, return raw response
        if response_model is None:
            return response

        # Parse response into Pydantic model
        try:
            return response_model.model_validate(response)
        except Exception as e:
            raise ValueError(f"Failed to parse response as {response_model.__name__}: {e}")
