from __future__ import annotations

"""
AdCP Python Client Library

Official Python client for the Ad Context Protocol (AdCP).
Supports both A2A and MCP protocols with full type safety.
"""

from adcp.adagents import (
    AuthorizationContext,
    domain_matches,
    fetch_adagents,
    fetch_agent_authorizations,
    get_all_properties,
    get_all_tags,
    get_properties_by_agent,
    identifiers_match,
    verify_agent_authorization,
    verify_agent_for_property,
)
from adcp.client import ADCPClient, ADCPMultiAgentClient
from adcp.exceptions import (
    AdagentsNotFoundError,
    AdagentsTimeoutError,
    AdagentsValidationError,
    ADCPAuthenticationError,
    ADCPConnectionError,
    ADCPError,
    ADCPProtocolError,
    ADCPTimeoutError,
    ADCPToolNotFoundError,
    ADCPWebhookError,
    ADCPWebhookSignatureError,
    RegistryError,
)
from adcp.registry import RegistryClient

# Test helpers
from adcp.testing import (
    CREATIVE_AGENT_CONFIG,
    TEST_AGENT_A2A_CONFIG,
    TEST_AGENT_A2A_NO_AUTH_CONFIG,
    TEST_AGENT_MCP_CONFIG,
    TEST_AGENT_MCP_NO_AUTH_CONFIG,
    TEST_AGENT_TOKEN,
    create_test_agent,
    creative_agent,
    test_agent,
    test_agent_a2a,
    test_agent_a2a_no_auth,
    test_agent_client,
    test_agent_no_auth,
)

# Re-export commonly-used request/response types for convenience
# Users should import from main package (e.g., `from adcp import GetProductsRequest`)
# rather than internal modules for better API stability
# Re-export core domain types and pricing options
# These are commonly used in typical workflows
from adcp.types import (
    # Account types
    AccountReference,
    # Type enums from PR #222
    AccountScope,
    # Audience & Targeting
    ActivateSignalRequest,
    ActivateSignalResponse,
    # Creative types
    ArtifactWebhookPayload,
    AssetContentType,
    AssetSelectors,
    AudienceSource,
    # Core domain types
    BrandManifest,
    BrandReference,
    # Creative Operations
    BuildCreativeRequest,
    BuildCreativeResponse,
    BuyingMode,
    # Catalog types
    Catalog,
    CatalogAction,
    CatalogFieldBinding,
    CatalogFieldMapping,
    CatalogGroupBinding,
    CatalogItemStatus,
    CatalogRequirements,
    CatalogType,
    ConsentBasis,
    ContentIdType,
    ContextObject,
    # Pricing options (all types for product creation)
    CpaPricingOption,
    CpcPricingOption,
    CpcvPricingOption,
    CpmAuctionPricingOption,
    CpmFixedRatePricingOption,
    CpmPricingOption,
    CppPricingOption,
    CpvPricingOption,
    # Media Buy Operations
    CreateMediaBuyRequest,
    CreateMediaBuyResponse,
    Creative,
    CreativeApproval,
    CreativeApprovalStatus,
    CreativeFilters,
    CreativeManifest,
    # Status enums (for control flow)
    CreativeStatus,
    CreativeVariant,
    DateRange,
    DatetimeRange,
    DeliverTo,
    DeliveryStatus,
    DevicePlatform,
    DeviceType,
    Duration,
    # Common data types
    Error,
    ErrorCode,
    EventType,
    ExtensionObject,
    FeedFormat,
    FlatRatePricingOption,
    Format,
    FormatCategory,
    FormatId,
    GeneratedTaskStatus,
    GetAccountFinancialsRequest,
    GetAccountFinancialsResponse,
    # Creative Delivery
    GetCreativeDeliveryRequest,
    GetCreativeDeliveryResponse,
    GetCreativeFeaturesRequest,
    GetCreativeFeaturesResponse,
    GetMediaBuyDeliveryRequest,
    GetMediaBuyDeliveryResponse,
    GetMediaBuysRequest,
    GetMediaBuysResponse,
    GetProductsRequest,
    GetProductsResponse,
    GetSignalsRequest,
    GetSignalsResponse,
    Gtin,
    # Account Operations
    ListAccountsRequest,
    ListAccountsResponse,
    ListAuthorizedPropertiesRequest,
    ListAuthorizedPropertiesResponse,
    ListCreativeFormatsRequest,
    ListCreativeFormatsResponse,
    ListCreativesRequest,
    ListCreativesResponse,
    # Event Operations
    LogEventRequest,
    LogEventResponse,
    McpWebhookPayload,
    Measurement,
    MediaBuy,
    MediaBuyDeliveryStatus,
    MediaBuyPackage,
    MediaBuyStatus,
    MediaChannel,
    OfferingAssetConstraint,
    OfferingAssetGroup,
    # Optimization
    OptimizationGoal,
    # Format overlays
    Overlay,
    Package,
    PackageRequest,
    PackageStatus,
    PaginationRequest,
    PreviewCreativeFormatRequest,
    PreviewCreativeInteractiveResponse,
    PreviewCreativeManifestRequest,
    PreviewCreativeRequest,
    PreviewCreativeResponse,
    PreviewCreativeStaticResponse,
    PriceGuidance,
    Pricing,
    PricingModel,
    Product,
    ProductFilters,
    PromotedOfferings,
    PromotedOfferingsAssetRequirements,
    PromotedOfferingsRequirement,
    PromotedProducts,
    Property,
    PropertyIdActivationKey,
    PropertyTagActivationKey,
    Proposal,
    ProvidePerformanceFeedbackRequest,
    ProvidePerformanceFeedbackResponse,
    PushNotificationConfig,
    Refine,
    ReportUsageRequest,
    ReportUsageResponse,
    SignalCatalogType,
    SignalFilters,
    SignalPricing,
    SignalPricingOption,
    Snapshot,
    SnapshotUnavailableReason,
    SyncAccountsRequest,
    SyncAccountsResponse,
    SyncAudiencesRequest,
    SyncAudiencesResponse,
    SyncCatalogsInputRequired,
    SyncCatalogsRequest,
    SyncCatalogsResponse,
    SyncCatalogsSubmitted,
    SyncCatalogsWorking,
    SyncCreativesRequest,
    SyncCreativesResponse,
    SyncEventSourcesRequest,
    SyncEventSourcesResponse,
    TimeBasedPricingOption,
    TimeUnit,
    Transform,
    UpdateFrequency,
    UpdateMediaBuyRequest,
    UpdateMediaBuyResponse,
    VcpmAuctionPricingOption,
    VcpmFixedRatePricingOption,
    VcpmPricingOption,
    WcagLevel,
    aliases,
)

# Import generated types modules - for internal use
# Note: Users should import specific types, not the whole module
from adcp.types import _generated as generated

# Re-export semantic type aliases for better ergonomics
from adcp.types.aliases import (
    AccountReferenceById,
    AccountReferenceByNaturalKey,
    ActivateSignalErrorResponse,
    ActivateSignalSuccessResponse,
    AgentDeployment,
    AgentDestination,
    AuthorizedAgent,
    AuthorizedAgentsByInlineProperties,
    AuthorizedAgentsByPropertyId,
    AuthorizedAgentsByPropertyTag,
    AuthorizedAgentsByPublisherProperties,
    AuthorizedAgentsBySignalId,
    AuthorizedAgentsBySignalTag,
    BothPreviewRender,
    BuildCreativeErrorResponse,
    BuildCreativeSuccessResponse,
    CalibrateContentErrorResponse,
    CalibrateContentSuccessResponse,
    CpmSignalPricingOption,
    CreateContentStandardsErrorResponse,
    CreateContentStandardsSuccessResponse,
    CreateMediaBuyErrorResponse,
    CreateMediaBuySuccessResponse,
    Deployment,
    Destination,
    FlatFeeSignalPricingOption,
    GetAccountFinancialsErrorResponse,
    GetAccountFinancialsSuccessResponse,
    GetContentStandardsErrorResponse,
    GetContentStandardsSuccessResponse,
    GetCreativeDeliveryByBuyerRefRequest,
    GetCreativeDeliveryByCreativeRequest,
    GetCreativeDeliveryByMediaBuyRequest,
    GetCreativeFeaturesErrorResponse,
    GetCreativeFeaturesSuccessResponse,
    GetMediaBuyArtifactsErrorResponse,
    GetMediaBuyArtifactsSuccessResponse,
    GetProductsBriefRequest,
    GetProductsRefineRequest,
    GetProductsWholesaleRequest,
    GetSignalsDiscoveryRequest,
    GetSignalsLookupRequest,
    HtmlPreviewRender,
    InlineDaastAsset,
    InlineVastAsset,
    KeyValueActivationKey,
    ListContentStandardsErrorResponse,
    ListContentStandardsSuccessResponse,
    LogEventErrorResponse,
    LogEventSuccessResponse,
    MediaSubAsset,
    PercentOfMediaSignalPricingOption,
    PlatformDeployment,
    PlatformDestination,
    PreviewCreativeBatchRequest,
    PreviewCreativeBatchResponse,
    PreviewCreativeSingleRequest,
    PreviewCreativeSingleResponse,
    PreviewCreativeVariantRequest,
    PreviewCreativeVariantResponse,
    PricingOption,
    PropertyId,
    PropertyTag,
    ProvidePerformanceFeedbackByBuyerRefRequest,
    ProvidePerformanceFeedbackByMediaBuyRequest,
    ProvidePerformanceFeedbackErrorResponse,
    ProvidePerformanceFeedbackSuccessResponse,
    PublisherProperties,
    PublisherPropertiesAll,
    PublisherPropertiesById,
    PublisherPropertiesByTag,
    SegmentIdActivationKey,
    SiSendActionResponseRequest,
    SiSendTextMessageRequest,
    SyncAccountsErrorResponse,
    SyncAccountsSuccessResponse,
    SyncAudiencesAudience,
    SyncAudiencesErrorResponse,
    SyncAudiencesSuccessResponse,
    SyncCatalogResult,
    SyncCatalogsErrorResponse,
    SyncCatalogsSuccessResponse,
    SyncCreativeResult,
    SyncCreativesErrorResponse,
    SyncCreativesSuccessResponse,
    SyncEventSourcesErrorResponse,
    SyncEventSourcesSuccessResponse,
    TextSubAsset,
    UpdateContentStandardsErrorResponse,
    UpdateContentStandardsSuccessResponse,
    UpdateMediaBuyErrorResponse,
    UpdateMediaBuyPackagesRequest,
    UpdateMediaBuyPropertiesRequest,
    UpdateMediaBuySuccessResponse,
    UrlDaastAsset,
    UrlPreviewRender,
    UrlVastAsset,
    ValidateContentDeliveryErrorResponse,
    ValidateContentDeliverySuccessResponse,
)
from adcp.types.core import (
    AgentConfig,
    Member,
    Protocol,
    ResolvedBrand,
    ResolvedProperty,
    TaskResult,
    TaskStatus,
    WebhookMetadata,
)
from adcp.utils import (
    get_asset_count,
    get_format_assets,
    get_individual_assets,
    get_optional_assets,
    get_repeatable_groups,
    get_required_assets,
    has_assets,
    normalize_assets_required,
    uses_deprecated_assets_field,
)
from adcp.validation import (
    ValidationError,
    validate_adagents,
    validate_agent_authorization,
    validate_product,
    validate_publisher_properties_item,
)
from adcp.webhooks import (
    create_a2a_webhook_payload,
    create_mcp_webhook_payload,
    extract_webhook_result_data,
    get_adcp_signed_headers_for_webhook,
)

__version__ = "3.7.0"


def get_adcp_version() -> str:
    """
    Get the target AdCP specification version this SDK is built for.

    This version determines which AdCP schemas are used for type generation
    and validation. The SDK is designed to work with this specific version
    of the AdCP specification.

    Returns:
        AdCP specification version (e.g., "2.5.0")

    Raises:
        FileNotFoundError: If ADCP_VERSION file is missing from package
    """
    from importlib.resources import files

    # Read from ADCP_VERSION file in package
    version_file = files("adcp") / "ADCP_VERSION"
    return version_file.read_text().strip()


__all__ = [
    # Version functions
    "get_adcp_version",
    # Client classes
    "ADCPClient",
    "ADCPMultiAgentClient",
    "RegistryClient",
    # Core types
    "AgentConfig",
    "Member",
    "Protocol",
    "ResolvedBrand",
    "ResolvedProperty",
    "TaskResult",
    "TaskStatus",
    "WebhookMetadata",
    # Webhook utilities
    "create_mcp_webhook_payload",
    "create_a2a_webhook_payload",
    "get_adcp_signed_headers_for_webhook",
    "extract_webhook_result_data",
    "McpWebhookPayload",
    # Account operations
    "AccountReference",
    "GetAccountFinancialsRequest",
    "GetAccountFinancialsResponse",
    "GetAccountFinancialsSuccessResponse",
    "GetAccountFinancialsErrorResponse",
    "ReportUsageRequest",
    "ReportUsageResponse",
    # Common request/response types (re-exported for convenience)
    "CreateMediaBuyRequest",
    "CreateMediaBuyResponse",
    "GetCreativeDeliveryRequest",
    "GetCreativeDeliveryResponse",
    "GetCreativeFeaturesRequest",
    "GetCreativeFeaturesResponse",
    "GetCreativeFeaturesSuccessResponse",
    "GetCreativeFeaturesErrorResponse",
    "GetMediaBuyDeliveryRequest",
    "GetMediaBuyDeliveryResponse",
    "GetMediaBuysRequest",
    "GetMediaBuysResponse",
    "GetProductsRequest",
    "GetProductsResponse",
    "UpdateMediaBuyRequest",
    "UpdateMediaBuyResponse",
    "BuildCreativeRequest",
    "BuildCreativeResponse",
    "ListAccountsRequest",
    "ListAccountsResponse",
    "ListCreativeFormatsRequest",
    "ListCreativeFormatsResponse",
    "ListCreativesRequest",
    "ListCreativesResponse",
    "LogEventRequest",
    "LogEventResponse",
    "PreviewCreativeRequest",
    "PreviewCreativeResponse",
    "SyncAccountsRequest",
    "SyncAccountsResponse",
    "SyncAudiencesRequest",
    "SyncAudiencesResponse",
    "SyncCatalogsInputRequired",
    "SyncCatalogsRequest",
    "SyncCatalogsResponse",
    "SyncCatalogsSubmitted",
    "SyncCatalogsWorking",
    "SyncCreativesRequest",
    "SyncCreativesResponse",
    "SyncEventSourcesRequest",
    "SyncEventSourcesResponse",
    "ActivateSignalRequest",
    "ActivateSignalResponse",
    "GetSignalsRequest",
    "GetSignalsResponse",
    "SignalFilters",
    "ProvidePerformanceFeedbackRequest",
    "ProvidePerformanceFeedbackResponse",
    "Error",
    "Format",
    "FormatId",
    "AssetContentType",
    "FormatCategory",
    "Product",
    "ProductFilters",
    # Catalog types
    "Catalog",
    "CatalogAction",
    "CatalogFieldBinding",
    "CatalogFieldMapping",
    "CatalogItemStatus",
    "CatalogRequirements",
    "CatalogType",
    "ConsentBasis",
    "ContentIdType",
    "FeedFormat",
    "Gtin",
    "OfferingAssetConstraint",
    "OfferingAssetGroup",
    "Transform",
    "UpdateFrequency",
    # Backward compat: these types were removed from upstream schemas
    "AssetSelectors",
    "PromotedOfferings",
    "PromotedOfferingsAssetRequirements",
    "PromotedOfferingsRequirement",
    "PromotedProducts",
    "Property",
    "SignalCatalogType",
    # Core domain types (from stable API)
    "AccountScope",
    "ArtifactWebhookPayload",
    "AudienceSource",
    "BrandManifest",
    "BrandReference",
    "BuyingMode",
    "CatalogGroupBinding",
    "ContextObject",
    "Creative",
    "CreativeApproval",
    "CreativeApprovalStatus",
    "CreativeFilters",
    "CreativeManifest",
    "CreativeVariant",
    "DateRange",
    "DatetimeRange",
    "DevicePlatform",
    "DeviceType",
    "Duration",
    "ErrorCode",
    "EventType",
    "ExtensionObject",
    "MediaBuy",
    "MediaBuyDeliveryStatus",
    "MediaBuyPackage",
    "MediaChannel",
    "OptimizationGoal",
    "Overlay",
    "Package",
    "PackageRequest",
    "PaginationRequest",
    "Proposal",
    "Refine",
    "Snapshot",
    "SnapshotUnavailableReason",
    "WcagLevel",
    # Status enums (for control flow)
    "CreativeStatus",
    "DeliveryStatus",
    "MediaBuyStatus",
    "PricingModel",
    # Pricing-related types
    "CpaPricingOption",
    "CpcPricingOption",
    "CpcvPricingOption",
    "CpmPricingOption",
    "CppPricingOption",
    "CpvPricingOption",
    "FlatRatePricingOption",
    "PriceGuidance",
    "TimeBasedPricingOption",
    "TimeUnit",
    "VcpmPricingOption",
    # Signal pricing types
    "SignalPricing",
    "SignalPricingOption",
    "CpmSignalPricingOption",
    "FlatFeeSignalPricingOption",
    "PercentOfMediaSignalPricingOption",
    # Configuration types
    "PushNotificationConfig",
    # Adagents validation
    "AuthorizationContext",
    "fetch_adagents",
    "fetch_agent_authorizations",
    "verify_agent_authorization",
    "verify_agent_for_property",
    "domain_matches",
    "identifiers_match",
    "get_all_properties",
    "get_all_tags",
    "get_properties_by_agent",
    # Test helpers
    "test_agent",
    "test_agent_a2a",
    "test_agent_no_auth",
    "test_agent_a2a_no_auth",
    "creative_agent",
    "test_agent_client",
    "create_test_agent",
    "TEST_AGENT_TOKEN",
    "TEST_AGENT_MCP_CONFIG",
    "TEST_AGENT_A2A_CONFIG",
    "TEST_AGENT_MCP_NO_AUTH_CONFIG",
    "TEST_AGENT_A2A_NO_AUTH_CONFIG",
    "CREATIVE_AGENT_CONFIG",
    # Exceptions
    "ADCPError",
    "ADCPConnectionError",
    "ADCPAuthenticationError",
    "ADCPTimeoutError",
    "ADCPProtocolError",
    "ADCPToolNotFoundError",
    "ADCPWebhookError",
    "ADCPWebhookSignatureError",
    "AdagentsValidationError",
    "AdagentsNotFoundError",
    "AdagentsTimeoutError",
    "RegistryError",
    # Validation utilities
    "ValidationError",
    "validate_adagents",
    "validate_agent_authorization",
    "validate_product",
    "validate_publisher_properties_item",
    # Format asset utilities
    "get_format_assets",
    "normalize_assets_required",
    "get_required_assets",
    "get_optional_assets",
    "get_individual_assets",
    "get_repeatable_groups",
    "uses_deprecated_assets_field",
    "get_asset_count",
    "has_assets",
    # Generated types modules
    "generated",
    "aliases",
    "GeneratedTaskStatus",
    # Semantic type aliases (for better API ergonomics)
    "AccountReferenceById",
    "AccountReferenceByNaturalKey",
    "ActivateSignalSuccessResponse",
    "ActivateSignalErrorResponse",
    "AgentDeployment",
    "AgentDestination",
    "AuthorizedAgent",
    "AuthorizedAgentsByInlineProperties",
    "AuthorizedAgentsByPropertyId",
    "AuthorizedAgentsByPropertyTag",
    "AuthorizedAgentsByPublisherProperties",
    "AuthorizedAgentsBySignalId",
    "AuthorizedAgentsBySignalTag",
    "BothPreviewRender",
    "BuildCreativeSuccessResponse",
    "BuildCreativeErrorResponse",
    "CalibrateContentSuccessResponse",
    "CalibrateContentErrorResponse",
    "CreateContentStandardsSuccessResponse",
    "CreateContentStandardsErrorResponse",
    "CreateMediaBuySuccessResponse",
    "CreateMediaBuyErrorResponse",
    "Deployment",
    "Destination",
    "GetContentStandardsSuccessResponse",
    "GetContentStandardsErrorResponse",
    "GetCreativeDeliveryByBuyerRefRequest",
    "GetCreativeDeliveryByCreativeRequest",
    "GetCreativeDeliveryByMediaBuyRequest",
    "GetMediaBuyArtifactsSuccessResponse",
    "GetMediaBuyArtifactsErrorResponse",
    "GetProductsBriefRequest",
    "GetProductsRefineRequest",
    "GetProductsWholesaleRequest",
    "GetSignalsDiscoveryRequest",
    "GetSignalsLookupRequest",
    "HtmlPreviewRender",
    "InlineDaastAsset",
    "InlineVastAsset",
    "KeyValueActivationKey",
    "ListContentStandardsSuccessResponse",
    "ListContentStandardsErrorResponse",
    "LogEventSuccessResponse",
    "LogEventErrorResponse",
    "MediaSubAsset",
    "PlatformDeployment",
    "PlatformDestination",
    "PreviewCreativeBatchRequest",
    "PreviewCreativeBatchResponse",
    "PreviewCreativeSingleRequest",
    "PreviewCreativeSingleResponse",
    "PreviewCreativeVariantRequest",
    "PreviewCreativeVariantResponse",
    "PricingOption",
    "PropertyId",
    "PropertyTag",
    "ProvidePerformanceFeedbackByBuyerRefRequest",
    "ProvidePerformanceFeedbackByMediaBuyRequest",
    "ProvidePerformanceFeedbackSuccessResponse",
    "ProvidePerformanceFeedbackErrorResponse",
    "PublisherProperties",
    "PublisherPropertiesAll",
    "PublisherPropertiesById",
    "PublisherPropertiesByTag",
    "SegmentIdActivationKey",
    "SiSendActionResponseRequest",
    "SiSendTextMessageRequest",
    "SyncAccountsSuccessResponse",
    "SyncAccountsErrorResponse",
    "SyncAudiencesAudience",
    "SyncAudiencesSuccessResponse",
    "SyncAudiencesErrorResponse",
    "SyncCatalogResult",
    "SyncCatalogsSuccessResponse",
    "SyncCatalogsErrorResponse",
    "SyncCreativeResult",
    "SyncCreativesSuccessResponse",
    "SyncCreativesErrorResponse",
    "SyncEventSourcesSuccessResponse",
    "SyncEventSourcesErrorResponse",
    "TextSubAsset",
    "UpdateContentStandardsSuccessResponse",
    "UpdateContentStandardsErrorResponse",
    "UpdateMediaBuySuccessResponse",
    "UpdateMediaBuyErrorResponse",
    "UpdateMediaBuyPackagesRequest",
    "UpdateMediaBuyPropertiesRequest",
    "UrlDaastAsset",
    "UrlPreviewRender",
    "UrlVastAsset",
    "ValidateContentDeliverySuccessResponse",
    "ValidateContentDeliveryErrorResponse",
    # Backward compat: renamed/removed types
    "DeliverTo",
    "Measurement",
    "Pricing",
    # Backward compat: pricing type renames
    "CpmAuctionPricingOption",
    "CpmFixedRatePricingOption",
    "VcpmAuctionPricingOption",
    "VcpmFixedRatePricingOption",
    # Backward compat: activation key schema change
    "PropertyIdActivationKey",
    "PropertyTagActivationKey",
    # Backward compat: preview alias renames
    "PreviewCreativeFormatRequest",
    "PreviewCreativeInteractiveResponse",
    "PreviewCreativeManifestRequest",
    "PreviewCreativeStaticResponse",
    # Backward compat: types removed from upstream schemas
    "ListAuthorizedPropertiesRequest",
    "ListAuthorizedPropertiesResponse",
    "PackageStatus",
]
