# 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.

"""
Response types for the Agentic Data Protocol (ADP).

This module defines the response types for all ADP methods.
"""

from typing import Any

from pydantic import ConfigDict, Field

from .common import (
    ExecutionMetadata,
    Implementation,
    IntentClass,
    Resource,
    ResourceId,
    ServerCapabilities,
    UsageContract,
    ValidationIssue,
)
from .jsonrpc import Cursor, Result


class PaginatedResult(Result):
    """Common result type for paginated responses."""

    next_cursor: Cursor | None = Field(
        default=None,
        alias="nextCursor",
        description="An opaque token representing the pagination position after the last returned result.",
    )


class InitializeResult(Result):
    """Response for the `adp.initialize` request."""

    model_config = ConfigDict(extra="allow", populate_by_name=True)

    protocol_version: str = Field(
        alias="protocolVersion", description="The version of the ADP protocol that the server wants to use."
    )
    capabilities: ServerCapabilities = Field(description="Server capabilities.")
    server_info: Implementation = Field(alias="serverInfo", description="Information about the server implementation.")
    instructions: str | None = Field(
        default=None, description="Instructions describing how to use the server and its features."
    )


class DiscoverResult(PaginatedResult):
    """Result of the `adp.discover` request."""

    resources: list[Resource] = Field(description="List of discovered resources matching the filter criteria.")


class DescribeResult(PaginatedResult):
    """Result of the `adp.describe` request."""

    resource_id: ResourceId = Field(alias="resourceId", description="The resource's full qualified identifier.")
    version: int = Field(description="The version of the resource schema that was returned.")
    intent_class: IntentClass = Field(alias="intentClass", description="The intent class this contract applies to.")
    usage_contract: UsageContract = Field(
        alias="usageContract", description="The usage contract for this resource and intent."
    )


class ValidateResult(Result):
    """Result of the `adp.validate` request."""

    valid: bool = Field(description="Whether the Intent IR is valid.")
    issues: list[ValidationIssue] | None = Field(default=None, description="List of validation issues, if any.")


class ExecuteResult(PaginatedResult):
    """Result of the `adp.execute` request."""

    results: list[dict[str, Any]] = Field(description="The result data.")
    execution_metadata: ExecutionMetadata | None = Field(
        default=None, alias="executionMetadata", description="Additional metadata about the execution."
    )
