# AUTO-GENERATED by scripts/generate_ergonomic_coercion.py
# Do not edit manually - changes will be overwritten on next type generation.
# To regenerate: python scripts/generate_types.py

# ruff: noqa: E501, I001
"""Apply type coercion to generated types for better ergonomics.

This module patches the generated types to accept more flexible input types
while maintaining type safety. It uses Pydantic's model_rebuild() to add
BeforeValidator annotations to fields.

Why import-time patching?
    We apply coercion at module load time rather than lazily because:
    1. Pydantic validation runs during __init__, before any lazy access
    2. model_rebuild() is the standard Pydantic pattern for post-hoc changes
    3. The cost is minimal (~10-20ms for all types, once at import)
    4. After import, there is zero runtime overhead
    5. This approach maintains full type checker compatibility

Coercion rules applied:
1. Enum fields accept string values (e.g., "video" for FormatCategory.video)
2. List[Enum] fields accept list of strings (e.g., ["image", "video"])
3. ContextObject fields accept dict values
4. ExtensionObject fields accept dict values
5. FieldModel (enum) lists accept string lists

Note: List variance issues (list[Subclass] not assignable to list[BaseClass])
are a fundamental Python typing limitation. Users extending library types
should use Sequence[T] in their own code or cast() for type checker appeasement.
"""

from __future__ import annotations

from typing import Annotated, Any

from pydantic import BeforeValidator

from adcp.types.coercion import (
    coerce_subclass_list,
    coerce_to_enum,
    coerce_to_enum_list,
    coerce_to_model,
)

from adcp.types.generated_poc.core.context import ContextObject
from adcp.types.generated_poc.core.creative_asset import CreativeAsset
from adcp.types.generated_poc.core.creative_assignment import CreativeAssignment
from adcp.types.generated_poc.core.error import Error
from adcp.types.generated_poc.core.ext import ExtensionObject
from adcp.types.generated_poc.core.format import Format
from adcp.types.generated_poc.core.package import Package
from adcp.types.generated_poc.core.product import Product
from adcp.types.generated_poc.enums.asset_content_type import AssetContentType
from adcp.types.generated_poc.enums.creative_sort_field import CreativeSortField
from adcp.types.generated_poc.enums.disclosure_position import DisclosurePosition
from adcp.types.generated_poc.enums.format_category import FormatCategory
from adcp.types.generated_poc.enums.pacing import Pacing
from adcp.types.generated_poc.enums.sort_direction import SortDirection
from adcp.types.generated_poc.enums.wcag_level import WcagLevel
from adcp.types.generated_poc.media_buy.create_media_buy_request import (
    CreateMediaBuyRequest,
)
from adcp.types.generated_poc.media_buy.list_creative_formats_request import (
    ListCreativeFormatsRequest,
)
from adcp.types.generated_poc.media_buy.list_creatives_request import (
    Field1,
    ListCreativesRequest,
    Sort,
)
from adcp.types.generated_poc.media_buy.package_request import PackageRequest
from adcp.types.generated_poc.media_buy.package_update import (
    PackageUpdate1,
    PackageUpdate2,
)
from adcp.types.generated_poc.media_buy.create_media_buy_response import (
    CreateMediaBuyResponse1,
)
from adcp.types.generated_poc.media_buy.get_media_buy_delivery_response import (
    GetMediaBuyDeliveryResponse,
    MediaBuyDelivery,
    NotificationType,
)
from adcp.types.generated_poc.media_buy.get_products_response import GetProductsResponse
from adcp.types.generated_poc.media_buy.list_creative_formats_response import (
    CreativeAgent,
    ListCreativeFormatsResponse,
)
from adcp.types.generated_poc.media_buy.list_creatives_response import (
    Creative,
    ListCreativesResponse,
)


def _apply_coercion() -> None:
    """Apply coercion validators to generated types.

    This function modifies the generated types in-place to accept
    more flexible input types.
    """
    # Apply coercion to ListCreativeFormatsRequest
    # - asset_types: list[AssetContentType | str] | None
    # - context: ContextObject | dict | None
    # - disclosure_positions: list[DisclosurePosition | str] | None
    # - ext: ExtensionObject | dict | None
    # - type: FormatCategory | str | None
    # - wcag_level: WcagLevel | str | None
    _patch_field_annotation(
        ListCreativeFormatsRequest,
        "asset_types",
        Annotated[
            list[AssetContentType] | None,
            BeforeValidator(coerce_to_enum_list(AssetContentType)),
        ],
    )
    _patch_field_annotation(
        ListCreativeFormatsRequest,
        "context",
        Annotated[ContextObject | None, BeforeValidator(coerce_to_model(ContextObject))],
    )
    _patch_field_annotation(
        ListCreativeFormatsRequest,
        "disclosure_positions",
        Annotated[
            list[DisclosurePosition] | None,
            BeforeValidator(coerce_to_enum_list(DisclosurePosition)),
        ],
    )
    _patch_field_annotation(
        ListCreativeFormatsRequest,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        ListCreativeFormatsRequest,
        "type",
        Annotated[FormatCategory | None, BeforeValidator(coerce_to_enum(FormatCategory))],
    )
    _patch_field_annotation(
        ListCreativeFormatsRequest,
        "wcag_level",
        Annotated[WcagLevel | None, BeforeValidator(coerce_to_enum(WcagLevel))],
    )
    ListCreativeFormatsRequest.model_rebuild(force=True)

    # Apply coercion to ListCreativesRequest
    # - context: ContextObject | dict | None
    # - ext: ExtensionObject | dict | None
    # - fields: list[Field1 | str] | None
    _patch_field_annotation(
        ListCreativesRequest,
        "context",
        Annotated[ContextObject | None, BeforeValidator(coerce_to_model(ContextObject))],
    )
    _patch_field_annotation(
        ListCreativesRequest,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        ListCreativesRequest,
        "fields",
        Annotated[
            list[Field1] | None,
            BeforeValidator(coerce_to_enum_list(Field1)),
        ],
    )
    ListCreativesRequest.model_rebuild(force=True)

    # Apply coercion to Sort
    # - direction: SortDirection | str | None
    # - field: CreativeSortField | str | None
    _patch_field_annotation(
        Sort,
        "direction",
        Annotated[SortDirection | None, BeforeValidator(coerce_to_enum(SortDirection))],
    )
    _patch_field_annotation(
        Sort,
        "field",
        Annotated[CreativeSortField | None, BeforeValidator(coerce_to_enum(CreativeSortField))],
    )
    Sort.model_rebuild(force=True)

    # Apply coercion to PackageRequest
    # - creative_assignments: list[CreativeAssignment] (accepts subclass instances)
    # - creatives: list[CreativeAsset] (accepts subclass instances)
    # - ext: ExtensionObject | dict | None
    # - pacing: Pacing | str | None
    _patch_field_annotation(
        PackageRequest,
        "creative_assignments",
        Annotated[
            list[CreativeAssignment] | None,
            BeforeValidator(coerce_subclass_list(CreativeAssignment)),
        ],
    )
    _patch_field_annotation(
        PackageRequest,
        "creatives",
        Annotated[
            list[CreativeAsset] | None,
            BeforeValidator(coerce_subclass_list(CreativeAsset)),
        ],
    )
    _patch_field_annotation(
        PackageRequest,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        PackageRequest,
        "pacing",
        Annotated[Pacing | None, BeforeValidator(coerce_to_enum(Pacing))],
    )
    PackageRequest.model_rebuild(force=True)

    # Apply coercion to CreateMediaBuyRequest
    # - context: ContextObject | dict | None
    # - ext: ExtensionObject | dict | None
    # - packages: list[PackageRequest] (accepts subclass instances)
    _patch_field_annotation(
        CreateMediaBuyRequest,
        "context",
        Annotated[ContextObject | None, BeforeValidator(coerce_to_model(ContextObject))],
    )
    _patch_field_annotation(
        CreateMediaBuyRequest,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        CreateMediaBuyRequest,
        "packages",
        Annotated[
            list[PackageRequest] | None,
            BeforeValidator(coerce_subclass_list(PackageRequest)),
        ],
    )
    CreateMediaBuyRequest.model_rebuild(force=True)

    # Apply coercion to PackageUpdate1
    # - creative_assignments: list[CreativeAssignment] (accepts subclass instances)
    # - creatives: list[CreativeAsset] (accepts subclass instances)
    # - ext: ExtensionObject | dict | None
    # - pacing: Pacing | str | None
    _patch_field_annotation(
        PackageUpdate1,
        "creative_assignments",
        Annotated[
            list[CreativeAssignment] | None,
            BeforeValidator(coerce_subclass_list(CreativeAssignment)),
        ],
    )
    _patch_field_annotation(
        PackageUpdate1,
        "creatives",
        Annotated[
            list[CreativeAsset] | None,
            BeforeValidator(coerce_subclass_list(CreativeAsset)),
        ],
    )
    _patch_field_annotation(
        PackageUpdate1,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        PackageUpdate1,
        "pacing",
        Annotated[Pacing | None, BeforeValidator(coerce_to_enum(Pacing))],
    )
    PackageUpdate1.model_rebuild(force=True)

    # Apply coercion to PackageUpdate2
    # - creative_assignments: list[CreativeAssignment] (accepts subclass instances)
    # - creatives: list[CreativeAsset] (accepts subclass instances)
    # - ext: ExtensionObject | dict | None
    # - pacing: Pacing | str | None
    _patch_field_annotation(
        PackageUpdate2,
        "creative_assignments",
        Annotated[
            list[CreativeAssignment] | None,
            BeforeValidator(coerce_subclass_list(CreativeAssignment)),
        ],
    )
    _patch_field_annotation(
        PackageUpdate2,
        "creatives",
        Annotated[
            list[CreativeAsset] | None,
            BeforeValidator(coerce_subclass_list(CreativeAsset)),
        ],
    )
    _patch_field_annotation(
        PackageUpdate2,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        PackageUpdate2,
        "pacing",
        Annotated[Pacing | None, BeforeValidator(coerce_to_enum(Pacing))],
    )
    PackageUpdate2.model_rebuild(force=True)

    # Apply coercion to GetProductsResponse
    # - context: ContextObject | dict | None
    # - errors: list[Error] (accepts subclass instances)
    # - ext: ExtensionObject | dict | None
    # - products: list[Product] (accepts subclass instances)
    _patch_field_annotation(
        GetProductsResponse,
        "context",
        Annotated[ContextObject | None, BeforeValidator(coerce_to_model(ContextObject))],
    )
    _patch_field_annotation(
        GetProductsResponse,
        "errors",
        Annotated[
            list[Error] | None,
            BeforeValidator(coerce_subclass_list(Error)),
        ],
    )
    _patch_field_annotation(
        GetProductsResponse,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        GetProductsResponse,
        "products",
        Annotated[
            list[Product],
            BeforeValidator(coerce_subclass_list(Product)),
        ],
    )
    GetProductsResponse.model_rebuild(force=True)

    # Apply coercion to ListCreativesResponse
    # - context: ContextObject | dict | None
    # - creatives: list[Creative] (accepts subclass instances)
    # - ext: ExtensionObject | dict | None
    _patch_field_annotation(
        ListCreativesResponse,
        "context",
        Annotated[ContextObject | None, BeforeValidator(coerce_to_model(ContextObject))],
    )
    _patch_field_annotation(
        ListCreativesResponse,
        "creatives",
        Annotated[
            list[Creative],
            BeforeValidator(coerce_subclass_list(Creative)),
        ],
    )
    _patch_field_annotation(
        ListCreativesResponse,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    ListCreativesResponse.model_rebuild(force=True)

    # Apply coercion to ListCreativeFormatsResponse
    # - context: ContextObject | dict | None
    # - creative_agents: list[CreativeAgent] (accepts subclass instances)
    # - errors: list[Error] (accepts subclass instances)
    # - ext: ExtensionObject | dict | None
    # - formats: list[Format] (accepts subclass instances)
    _patch_field_annotation(
        ListCreativeFormatsResponse,
        "context",
        Annotated[ContextObject | None, BeforeValidator(coerce_to_model(ContextObject))],
    )
    _patch_field_annotation(
        ListCreativeFormatsResponse,
        "creative_agents",
        Annotated[
            list[CreativeAgent] | None,
            BeforeValidator(coerce_subclass_list(CreativeAgent)),
        ],
    )
    _patch_field_annotation(
        ListCreativeFormatsResponse,
        "errors",
        Annotated[
            list[Error] | None,
            BeforeValidator(coerce_subclass_list(Error)),
        ],
    )
    _patch_field_annotation(
        ListCreativeFormatsResponse,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        ListCreativeFormatsResponse,
        "formats",
        Annotated[
            list[Format],
            BeforeValidator(coerce_subclass_list(Format)),
        ],
    )
    ListCreativeFormatsResponse.model_rebuild(force=True)

    # Apply coercion to CreateMediaBuyResponse1
    # - context: ContextObject | dict | None
    # - ext: ExtensionObject | dict | None
    # - packages: list[Package] (accepts subclass instances)
    _patch_field_annotation(
        CreateMediaBuyResponse1,
        "context",
        Annotated[ContextObject | None, BeforeValidator(coerce_to_model(ContextObject))],
    )
    _patch_field_annotation(
        CreateMediaBuyResponse1,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        CreateMediaBuyResponse1,
        "packages",
        Annotated[
            list[Package],
            BeforeValidator(coerce_subclass_list(Package)),
        ],
    )
    CreateMediaBuyResponse1.model_rebuild(force=True)

    # Apply coercion to GetMediaBuyDeliveryResponse
    # - context: ContextObject | dict | None
    # - errors: list[Error] (accepts subclass instances)
    # - ext: ExtensionObject | dict | None
    # - media_buy_deliveries: list[MediaBuyDelivery] (accepts subclass instances)
    # - notification_type: NotificationType | str | None
    _patch_field_annotation(
        GetMediaBuyDeliveryResponse,
        "context",
        Annotated[ContextObject | None, BeforeValidator(coerce_to_model(ContextObject))],
    )
    _patch_field_annotation(
        GetMediaBuyDeliveryResponse,
        "errors",
        Annotated[
            list[Error] | None,
            BeforeValidator(coerce_subclass_list(Error)),
        ],
    )
    _patch_field_annotation(
        GetMediaBuyDeliveryResponse,
        "ext",
        Annotated[ExtensionObject | None, BeforeValidator(coerce_to_model(ExtensionObject))],
    )
    _patch_field_annotation(
        GetMediaBuyDeliveryResponse,
        "media_buy_deliveries",
        Annotated[
            list[MediaBuyDelivery],
            BeforeValidator(coerce_subclass_list(MediaBuyDelivery)),
        ],
    )
    _patch_field_annotation(
        GetMediaBuyDeliveryResponse,
        "notification_type",
        Annotated[NotificationType | None, BeforeValidator(coerce_to_enum(NotificationType))],
    )
    GetMediaBuyDeliveryResponse.model_rebuild(force=True)


def _patch_field_annotation(
    model: type,
    field_name: str,
    new_annotation: Any,
) -> None:
    """Patch a field annotation on a Pydantic model.

    This modifies the model's __annotations__ dict to add
    BeforeValidator coercion.
    """
    model.__annotations__[field_name] = new_annotation


# Apply coercion when module is imported
_apply_coercion()
