# Copyright 2026 Datastrato, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""ADP SDK - Python SDK for the Agentic Data Protocol."""

from collections.abc import AsyncIterator
from contextlib import asynccontextmanager

from adp_sdk.client import ClientSession, basic_auth
from adp_sdk.shared import (
    ADPError,
    ExecutionFailedError,
    InternalError,
    InvalidParamsError,
    InvalidRequestError,
    MethodNotFoundError,
    ParseError,
    ResourceNotFoundError,
    UnauthorizedError,
    ValidationFailedError,
)
from adp_sdk.transports import Transport
from adp_sdk.transports.stdio import StdioClientTransport
from adp_sdk.types import LATEST_PROTOCOL_VERSION
from adp_sdk.types.common import (
    Capabilities,
    FieldDefinition,
    IdentityPredicate,
    IntentClass,
    Predicate,
    PredicateExpression,
    PredicateGroup,
    Resource,
    UsageContract,
    ValidationIssue,
)
from adp_sdk.types.intents import (
    IngestIntent,
    Intent,
    LookupIntent,
    QueryIntent,
    ReviseIntent,
)
from adp_sdk.types.responses import (
    DescribeResult,
    DiscoverResult,
    ExecuteResult,
    InitializeResult,
    ValidateResult,
)

__version__ = "0.1.0"


@asynccontextmanager
async def stdio_client(
    command: str,
    args: list[str] | None = None,
    env: dict[str, str] | None = None,
    cwd: str | None = None,
    authorization: str | None = None,
) -> AsyncIterator[ClientSession]:
    """Create a :class:`ClientSession` connected via stdio transport.

    Launches *command* as a subprocess and communicates over
    stdin/stdout using newline-delimited JSON.

    Usage::

        async with stdio_client("python", ["-m", "adp_hypervisor"]) as session:
            resources = await session.discover()

    Args:
        command: The executable to run.
        args: Command-line arguments.
        env: Additional environment variables.
        cwd: Working directory for the subprocess.
        authorization: Optional authorization header value for requests
            (e.g. ``basic_auth("user", "pass")``).

    Yields:
        An initialized :class:`ClientSession`.
    """
    transport = StdioClientTransport(command, args=args, env=env, cwd=cwd)
    async with ClientSession(transport, authorization=authorization) as session:
        yield session


__all__ = [
    "__version__",
    "LATEST_PROTOCOL_VERSION",
    # Convenience
    "stdio_client",
    "basic_auth",
    # Core classes
    "ClientSession",
    "Transport",
    "StdioClientTransport",
    # Protocol types
    "Capabilities",
    "DescribeResult",
    "DiscoverResult",
    "ExecuteResult",
    "FieldDefinition",
    "IdentityPredicate",
    "IngestIntent",
    "InitializeResult",
    "Intent",
    "IntentClass",
    "LookupIntent",
    "Predicate",
    "PredicateExpression",
    "PredicateGroup",
    "QueryIntent",
    "Resource",
    "ReviseIntent",
    "UsageContract",
    "ValidateResult",
    "ValidationIssue",
    # Exceptions
    "ADPError",
    "ExecutionFailedError",
    "InternalError",
    "InvalidParamsError",
    "InvalidRequestError",
    "MethodNotFoundError",
    "ParseError",
    "ResourceNotFoundError",
    "UnauthorizedError",
    "ValidationFailedError",
]
