"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .adgroupassociatebidlist import (
    AdGroupAssociateBidList,
    AdGroupAssociateBidListTypedDict,
)
from .adgroupaudiencetargeting import (
    AdGroupAudienceTargeting,
    AdGroupAudienceTargetingTypedDict,
)
from .adgroupbudget import AdGroupBudget, AdGroupBudgetTypedDict
from .adgroupchannel import AdGroupChannel
from .adgroupcomscoresettings import (
    AdGroupComscoreSettings,
    AdGroupComscoreSettingsTypedDict,
)
from .adgroupcontracttargeting import (
    AdGroupContractTargeting,
    AdGroupContractTargetingTypedDict,
)
from .adgroupflight import AdGroupFlight, AdGroupFlightTypedDict
from .adgroupfunnellocation import AdGroupFunnelLocation
from .adgroupkoaoptimizationsettings import (
    AdGroupKoaOptimizationSettings,
    AdGroupKoaOptimizationSettingsTypedDict,
)
from .adgroupnielsentrackingattributes import (
    AdGroupNielsenTrackingAttributes,
    AdGroupNielsenTrackingAttributesTypedDict,
)
from .adgrouproigoal import AdGroupROIGoal, AdGroupROIGoalTypedDict
from .dimensionalbiddingdimensions import DimensionalBiddingDimensions
import pydantic
from pydantic import model_serializer
from ttd_workflows.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from typing import List, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class AdGroupWorkflowTypedDict(TypedDict):
    id: Nullable[str]
    name: Nullable[str]
    campaign_id: Nullable[str]
    is_enabled: bool
    channel: AdGroupChannel
    funnel_location: AdGroupFunnelLocation
    budget: AdGroupBudgetTypedDict
    roi_goal: AdGroupROIGoalTypedDict
    creative_ids: Nullable[List[str]]
    associated_bid_lists: Nullable[List[AdGroupAssociateBidListTypedDict]]
    flights: Nullable[List[AdGroupFlightTypedDict]]
    comscore_settings: AdGroupComscoreSettingsTypedDict
    contract_targeting: AdGroupContractTargetingTypedDict
    dimensional_bidding_auto_optimization_settings: Nullable[
        List[List[DimensionalBiddingDimensions]]
    ]
    is_use_clicks_as_conversions_enabled: bool
    is_use_secondary_conversions_enabled: bool
    description: NotRequired[Nullable[str]]
    programmatic_guaranteed_private_contract_id: NotRequired[Nullable[str]]
    base_bid_cpm_in_advertiser_currency: NotRequired[Nullable[float]]
    max_bid_cpm_in_advertiser_currency: NotRequired[Nullable[float]]
    audience_targeting: NotRequired[AdGroupAudienceTargetingTypedDict]
    koa_optimization_settings: NotRequired[AdGroupKoaOptimizationSettingsTypedDict]
    nielsen_tracking_attributes: NotRequired[AdGroupNielsenTrackingAttributesTypedDict]


class AdGroupWorkflow(BaseModel):
    id: Nullable[str]

    name: Nullable[str]

    campaign_id: Annotated[Nullable[str], pydantic.Field(alias="campaignId")]

    is_enabled: Annotated[bool, pydantic.Field(alias="isEnabled")]

    channel: AdGroupChannel

    funnel_location: Annotated[
        AdGroupFunnelLocation, pydantic.Field(alias="funnelLocation")
    ]

    budget: AdGroupBudget

    roi_goal: Annotated[AdGroupROIGoal, pydantic.Field(alias="roiGoal")]

    creative_ids: Annotated[Nullable[List[str]], pydantic.Field(alias="creativeIds")]

    associated_bid_lists: Annotated[
        Nullable[List[AdGroupAssociateBidList]],
        pydantic.Field(alias="associatedBidLists"),
    ]

    flights: Nullable[List[AdGroupFlight]]

    comscore_settings: Annotated[
        AdGroupComscoreSettings, pydantic.Field(alias="comscoreSettings")
    ]

    contract_targeting: Annotated[
        AdGroupContractTargeting, pydantic.Field(alias="contractTargeting")
    ]

    dimensional_bidding_auto_optimization_settings: Annotated[
        Nullable[List[List[DimensionalBiddingDimensions]]],
        pydantic.Field(alias="dimensionalBiddingAutoOptimizationSettings"),
    ]

    is_use_clicks_as_conversions_enabled: Annotated[
        bool, pydantic.Field(alias="isUseClicksAsConversionsEnabled")
    ]

    is_use_secondary_conversions_enabled: Annotated[
        bool, pydantic.Field(alias="isUseSecondaryConversionsEnabled")
    ]

    description: OptionalNullable[str] = UNSET

    programmatic_guaranteed_private_contract_id: Annotated[
        OptionalNullable[str],
        pydantic.Field(alias="programmaticGuaranteedPrivateContractId"),
    ] = UNSET

    base_bid_cpm_in_advertiser_currency: Annotated[
        OptionalNullable[float], pydantic.Field(alias="baseBidCPMInAdvertiserCurrency")
    ] = UNSET

    max_bid_cpm_in_advertiser_currency: Annotated[
        OptionalNullable[float], pydantic.Field(alias="maxBidCPMInAdvertiserCurrency")
    ] = UNSET

    audience_targeting: Annotated[
        Optional[AdGroupAudienceTargeting], pydantic.Field(alias="audienceTargeting")
    ] = None

    koa_optimization_settings: Annotated[
        Optional[AdGroupKoaOptimizationSettings],
        pydantic.Field(alias="koaOptimizationSettings"),
    ] = None

    nielsen_tracking_attributes: Annotated[
        Optional[AdGroupNielsenTrackingAttributes],
        pydantic.Field(alias="nielsenTrackingAttributes"),
    ] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "description",
            "programmaticGuaranteedPrivateContractId",
            "baseBidCPMInAdvertiserCurrency",
            "maxBidCPMInAdvertiserCurrency",
            "audienceTargeting",
            "koaOptimizationSettings",
            "nielsenTrackingAttributes",
        ]
        nullable_fields = [
            "id",
            "name",
            "campaignId",
            "description",
            "programmaticGuaranteedPrivateContractId",
            "baseBidCPMInAdvertiserCurrency",
            "maxBidCPMInAdvertiserCurrency",
            "creativeIds",
            "associatedBidLists",
            "flights",
            "dimensionalBiddingAutoOptimizationSettings",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
