"""
Layer 0 Agent: Environment-based client wrapper for spicedb
Automatically loads credentials from environment variables for easy integration
with AI Agent frameworks (LangChain, CrewAI, Madison, etc.)
"""
import os
from typing import Any, Dict, List, Literal, Optional, Union
from .raw import *


class RawAgent:
    """
    Agent-ready wrapper for spicedb raw API client.

    Automatically loads credentials from environment variables:
    - SPICEDB_API_KEY: API key/token for authentication
    - SPICEDB_BASE_URL: Base URL for the API (optional, has default)

    Usage:
        agent = RawAgent()  # Uses env vars
        agent = RawAgent(api_key="...", base_url="...")  # Explicit
    """

    def __init__(
        self,
        api_key: Optional[str] = None,
        base_url: Optional[str] = None
    ):
        # Load from environment variables with fallbacks
        self.api_key = api_key or os.environ.get("SPICEDB_API_KEY")
        self.base_url = base_url or os.environ.get("SPICEDB_BASE_URL", "https://api.example.com")

        if not self.api_key:
            raise ValueError(
                "API key is required. Set SPICEDB_API_KEY environment variable "
                "or pass api_key parameter."
            )

        # Initialize raw client(s)
        self._experimental = ExperimentalClient(
            base_url=self.base_url,
            api_token=self.api_key
        )
        self._deprecated = DeprecatedClient(
            base_url=self.base_url,
            api_token=self.api_key
        )
        self._permissions = PermissionsClient(
            base_url=self.base_url,
            api_token=self.api_key
        )
        self._schema = SchemaClient(
            base_url=self.base_url,
            api_token=self.api_key
        )
        self._watch = WatchClient(
            base_url=self.base_url,
            api_token=self.api_key
        )

    # ─────────────────────────────────────────────────────────────────────────────
    # Experimental operations
    # ─────────────────────────────────────────────────────────────────────────────

    async def experimental_service_experimental_count_relationships(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_experimental_count_relationships"""
        return await self._experimental.experimental_service_experimental_count_relationships(body=body)

    async def experimental_service_experimental_register_relationship_counter(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_experimental_register_relationship_counter"""
        return await self._experimental.experimental_service_experimental_register_relationship_counter(body=body)

    async def experimental_service_experimental_unregister_relationship_counter(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_experimental_unregister_relationship_counter"""
        return await self._experimental.experimental_service_experimental_unregister_relationship_counter(body=body)

    # ─────────────────────────────────────────────────────────────────────────────
    # Deprecated operations
    # ─────────────────────────────────────────────────────────────────────────────

    async def experimental_service_experimental_diff_schema(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_experimental_diff_schema"""
        return await self._deprecated.experimental_service_experimental_diff_schema(body=body)

    async def experimental_service_bulk_check_permission(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_bulk_check_permission"""
        return await self._deprecated.experimental_service_bulk_check_permission(body=body)

    async def experimental_service_experimental_computable_permissions(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_experimental_computable_permissions"""
        return await self._deprecated.experimental_service_experimental_computable_permissions(body=body)

    async def experimental_service_experimental_dependent_relations(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_experimental_dependent_relations"""
        return await self._deprecated.experimental_service_experimental_dependent_relations(body=body)

    async def experimental_service_experimental_reflect_schema(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_experimental_reflect_schema"""
        return await self._deprecated.experimental_service_experimental_reflect_schema(body=body)

    async def experimental_service_bulk_export_relationships(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_bulk_export_relationships"""
        return await self._deprecated.experimental_service_bulk_export_relationships(body=body)

    async def experimental_service_bulk_import_relationships(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """experimental_service_bulk_import_relationships"""
        return await self._deprecated.experimental_service_bulk_import_relationships(body=body)

    # ─────────────────────────────────────────────────────────────────────────────
    # Permissions operations
    # ─────────────────────────────────────────────────────────────────────────────

    async def permissions_service_check_permission(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_check_permission"""
        return await self._permissions.permissions_service_check_permission(body=body)

    async def permissions_service_check_bulk_permissions(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_check_bulk_permissions"""
        return await self._permissions.permissions_service_check_bulk_permissions(body=body)

    async def permissions_service_expand_permission_tree(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_expand_permission_tree"""
        return await self._permissions.permissions_service_expand_permission_tree(body=body)

    async def permissions_service_lookup_resources(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_lookup_resources"""
        return await self._permissions.permissions_service_lookup_resources(body=body)

    async def permissions_service_lookup_subjects(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_lookup_subjects"""
        return await self._permissions.permissions_service_lookup_subjects(body=body)

    async def permissions_service_delete_relationships(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_delete_relationships"""
        return await self._permissions.permissions_service_delete_relationships(body=body)

    async def permissions_service_export_bulk_relationships(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_export_bulk_relationships"""
        return await self._permissions.permissions_service_export_bulk_relationships(body=body)

    async def permissions_service_import_bulk_relationships(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_import_bulk_relationships"""
        return await self._permissions.permissions_service_import_bulk_relationships(body=body)

    async def permissions_service_read_relationships(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_read_relationships"""
        return await self._permissions.permissions_service_read_relationships(body=body)

    async def permissions_service_write_relationships(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """permissions_service_write_relationships"""
        return await self._permissions.permissions_service_write_relationships(body=body)

    # ─────────────────────────────────────────────────────────────────────────────
    # Schema operations
    # ─────────────────────────────────────────────────────────────────────────────

    async def schema_service_diff_schema(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """schema_service_diff_schema"""
        return await self._schema.schema_service_diff_schema(body=body)

    async def schema_service_computable_permissions(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """schema_service_computable_permissions"""
        return await self._schema.schema_service_computable_permissions(body=body)

    async def schema_service_dependent_relations(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """schema_service_dependent_relations"""
        return await self._schema.schema_service_dependent_relations(body=body)

    async def schema_service_read_schema(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """Errors include:
- INVALID_ARGUMENT: a provided value has failed to semantically validate
- NOT_FOUND: no schema has been defined"""
        return await self._schema.schema_service_read_schema(body=body)

    async def schema_service_reflect_schema(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """schema_service_reflect_schema"""
        return await self._schema.schema_service_reflect_schema(body=body)

    async def schema_service_write_schema(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """schema_service_write_schema"""
        return await self._schema.schema_service_write_schema(body=body)

    # ─────────────────────────────────────────────────────────────────────────────
    # Watch operations
    # ─────────────────────────────────────────────────────────────────────────────

    async def watch_service_watch(self, body: Dict[str, Any]) -> Dict[str, Any]:
        """watch_service_watch"""
        return await self._watch.watch_service_watch(body=body)
