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

from __future__ import annotations
from dub.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
import pydantic
from pydantic import model_serializer
from typing import Dict, List, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


UpsertLinkTagIdsTypedDict = TypeAliasType(
    "UpsertLinkTagIdsTypedDict", Union[str, List[str]]
)
r"""The unique IDs of the tags assigned to the short link."""


UpsertLinkTagIds = TypeAliasType("UpsertLinkTagIds", Union[str, List[str]])
r"""The unique IDs of the tags assigned to the short link."""


UpsertLinkTagNamesTypedDict = TypeAliasType(
    "UpsertLinkTagNamesTypedDict", Union[str, List[str]]
)
r"""The unique name of the tags assigned to the short link (case insensitive)."""


UpsertLinkTagNames = TypeAliasType("UpsertLinkTagNames", Union[str, List[str]])
r"""The unique name of the tags assigned to the short link (case insensitive)."""


class UpsertLinkTestVariantsTypedDict(TypedDict):
    url: str
    percentage: float


class UpsertLinkTestVariants(BaseModel):
    url: str

    percentage: float


class UpsertLinkRequestBodyTypedDict(TypedDict):
    url: str
    r"""The destination URL of the short link."""
    domain: NotRequired[str]
    r"""The domain of the short link (without protocol). If not provided, the primary domain for the workspace will be used (or `dub.sh` if the workspace has no domains)."""
    key: NotRequired[str]
    r"""The short link slug. If not provided, a random 7-character slug will be generated."""
    key_length: NotRequired[float]
    r"""The length of the short link slug. Defaults to 7 if not provided. When used with `prefix`, the total length of the key will be `prefix.length + keyLength`."""
    external_id: NotRequired[Nullable[str]]
    r"""The ID of the link in your database. If set, it can be used to identify the link in future API requests (must be prefixed with 'ext_' when passed as a query parameter). This key is unique across your workspace."""
    tenant_id: NotRequired[Nullable[str]]
    r"""The ID of the tenant that created the link inside your system. If set, it can be used to fetch all links for a tenant."""
    program_id: NotRequired[Nullable[str]]
    r"""The ID of the program the short link is associated with."""
    partner_id: NotRequired[Nullable[str]]
    r"""The ID of the partner the short link is associated with."""
    prefix: NotRequired[str]
    r"""The prefix of the short link slug for randomly-generated keys (e.g. if prefix is `/c/`, generated keys will be in the `/c/:key` format). Will be ignored if `key` is provided."""
    track_conversion: NotRequired[bool]
    r"""Whether to track conversions for the short link. Defaults to `false` if not provided."""
    archived: NotRequired[bool]
    r"""Whether the short link is archived. Defaults to `false` if not provided."""
    tag_ids: NotRequired[UpsertLinkTagIdsTypedDict]
    r"""The unique IDs of the tags assigned to the short link."""
    tag_names: NotRequired[UpsertLinkTagNamesTypedDict]
    r"""The unique name of the tags assigned to the short link (case insensitive)."""
    folder_id: NotRequired[Nullable[str]]
    r"""The unique ID existing folder to assign the short link to."""
    comments: NotRequired[Nullable[str]]
    r"""The comments for the short link."""
    expires_at: NotRequired[Nullable[str]]
    r"""The date and time when the short link will expire at."""
    expired_url: NotRequired[Nullable[str]]
    r"""The URL to redirect to when the short link has expired."""
    password: NotRequired[Nullable[str]]
    r"""The password required to access the destination URL of the short link."""
    proxy: NotRequired[bool]
    r"""Whether the short link uses Custom Link Previews feature. Defaults to `false` if not provided."""
    title: NotRequired[Nullable[str]]
    r"""The custom link preview title (og:title). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og"""
    description: NotRequired[Nullable[str]]
    r"""The custom link preview description (og:description). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og"""
    image: NotRequired[Nullable[str]]
    r"""The custom link preview image (og:image). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og"""
    video: NotRequired[Nullable[str]]
    r"""The custom link preview video (og:video). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og"""
    rewrite: NotRequired[bool]
    r"""Whether the short link uses link cloaking. Defaults to `false` if not provided."""
    ios: NotRequired[Nullable[str]]
    r"""The iOS destination URL for the short link for iOS device targeting."""
    android: NotRequired[Nullable[str]]
    r"""The Android destination URL for the short link for Android device targeting."""
    geo: NotRequired[Nullable[Dict[str, str]]]
    r"""Geo targeting information for the short link in JSON format `{[COUNTRY]: https://example.com }`. See https://d.to/geo for more information."""
    do_index: NotRequired[bool]
    r"""Allow search engines to index your short link. Defaults to `false` if not provided. Learn more: https://d.to/noindex"""
    utm_source: NotRequired[Nullable[str]]
    r"""The UTM source of the short link. If set, this will populate or override the UTM source in the destination URL."""
    utm_medium: NotRequired[Nullable[str]]
    r"""The UTM medium of the short link. If set, this will populate or override the UTM medium in the destination URL."""
    utm_campaign: NotRequired[Nullable[str]]
    r"""The UTM campaign of the short link. If set, this will populate or override the UTM campaign in the destination URL."""
    utm_term: NotRequired[Nullable[str]]
    r"""The UTM term of the short link. If set, this will populate or override the UTM term in the destination URL."""
    utm_content: NotRequired[Nullable[str]]
    r"""The UTM content of the short link. If set, this will populate or override the UTM content in the destination URL."""
    ref: NotRequired[Nullable[str]]
    r"""The referral tag of the short link. If set, this will populate or override the `ref` query parameter in the destination URL."""
    webhook_ids: NotRequired[Nullable[List[str]]]
    r"""An array of webhook IDs to trigger when the link is clicked. These webhooks will receive click event data."""
    test_variants: NotRequired[Nullable[List[UpsertLinkTestVariantsTypedDict]]]
    r"""An array of A/B test URLs and the percentage of traffic to send to each URL."""
    test_started_at: NotRequired[Nullable[str]]
    r"""The date and time when the tests started."""
    test_completed_at: NotRequired[Nullable[str]]
    r"""The date and time when the tests were or will be completed."""
    public_stats: NotRequired[bool]
    r"""Deprecated: Use `dashboard` instead. Whether the short link's stats are publicly accessible. Defaults to `false` if not provided."""
    tag_id: NotRequired[Nullable[str]]
    r"""Deprecated: Use `tagIds` instead. The unique ID of the tag assigned to the short link."""


class UpsertLinkRequestBody(BaseModel):
    url: str
    r"""The destination URL of the short link."""

    domain: Optional[str] = None
    r"""The domain of the short link (without protocol). If not provided, the primary domain for the workspace will be used (or `dub.sh` if the workspace has no domains)."""

    key: Optional[str] = None
    r"""The short link slug. If not provided, a random 7-character slug will be generated."""

    key_length: Annotated[Optional[float], pydantic.Field(alias="keyLength")] = None
    r"""The length of the short link slug. Defaults to 7 if not provided. When used with `prefix`, the total length of the key will be `prefix.length + keyLength`."""

    external_id: Annotated[
        OptionalNullable[str], pydantic.Field(alias="externalId")
    ] = UNSET
    r"""The ID of the link in your database. If set, it can be used to identify the link in future API requests (must be prefixed with 'ext_' when passed as a query parameter). This key is unique across your workspace."""

    tenant_id: Annotated[OptionalNullable[str], pydantic.Field(alias="tenantId")] = (
        UNSET
    )
    r"""The ID of the tenant that created the link inside your system. If set, it can be used to fetch all links for a tenant."""

    program_id: Annotated[OptionalNullable[str], pydantic.Field(alias="programId")] = (
        UNSET
    )
    r"""The ID of the program the short link is associated with."""

    partner_id: Annotated[OptionalNullable[str], pydantic.Field(alias="partnerId")] = (
        UNSET
    )
    r"""The ID of the partner the short link is associated with."""

    prefix: Optional[str] = None
    r"""The prefix of the short link slug for randomly-generated keys (e.g. if prefix is `/c/`, generated keys will be in the `/c/:key` format). Will be ignored if `key` is provided."""

    track_conversion: Annotated[
        Optional[bool], pydantic.Field(alias="trackConversion")
    ] = None
    r"""Whether to track conversions for the short link. Defaults to `false` if not provided."""

    archived: Optional[bool] = None
    r"""Whether the short link is archived. Defaults to `false` if not provided."""

    tag_ids: Annotated[Optional[UpsertLinkTagIds], pydantic.Field(alias="tagIds")] = (
        None
    )
    r"""The unique IDs of the tags assigned to the short link."""

    tag_names: Annotated[
        Optional[UpsertLinkTagNames], pydantic.Field(alias="tagNames")
    ] = None
    r"""The unique name of the tags assigned to the short link (case insensitive)."""

    folder_id: Annotated[OptionalNullable[str], pydantic.Field(alias="folderId")] = (
        UNSET
    )
    r"""The unique ID existing folder to assign the short link to."""

    comments: OptionalNullable[str] = UNSET
    r"""The comments for the short link."""

    expires_at: Annotated[OptionalNullable[str], pydantic.Field(alias="expiresAt")] = (
        UNSET
    )
    r"""The date and time when the short link will expire at."""

    expired_url: Annotated[
        OptionalNullable[str], pydantic.Field(alias="expiredUrl")
    ] = UNSET
    r"""The URL to redirect to when the short link has expired."""

    password: OptionalNullable[str] = UNSET
    r"""The password required to access the destination URL of the short link."""

    proxy: Optional[bool] = None
    r"""Whether the short link uses Custom Link Previews feature. Defaults to `false` if not provided."""

    title: OptionalNullable[str] = UNSET
    r"""The custom link preview title (og:title). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og"""

    description: OptionalNullable[str] = UNSET
    r"""The custom link preview description (og:description). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og"""

    image: OptionalNullable[str] = UNSET
    r"""The custom link preview image (og:image). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og"""

    video: OptionalNullable[str] = UNSET
    r"""The custom link preview video (og:video). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og"""

    rewrite: Optional[bool] = None
    r"""Whether the short link uses link cloaking. Defaults to `false` if not provided."""

    ios: OptionalNullable[str] = UNSET
    r"""The iOS destination URL for the short link for iOS device targeting."""

    android: OptionalNullable[str] = UNSET
    r"""The Android destination URL for the short link for Android device targeting."""

    geo: OptionalNullable[Dict[str, str]] = UNSET
    r"""Geo targeting information for the short link in JSON format `{[COUNTRY]: https://example.com }`. See https://d.to/geo for more information."""

    do_index: Annotated[Optional[bool], pydantic.Field(alias="doIndex")] = None
    r"""Allow search engines to index your short link. Defaults to `false` if not provided. Learn more: https://d.to/noindex"""

    utm_source: OptionalNullable[str] = UNSET
    r"""The UTM source of the short link. If set, this will populate or override the UTM source in the destination URL."""

    utm_medium: OptionalNullable[str] = UNSET
    r"""The UTM medium of the short link. If set, this will populate or override the UTM medium in the destination URL."""

    utm_campaign: OptionalNullable[str] = UNSET
    r"""The UTM campaign of the short link. If set, this will populate or override the UTM campaign in the destination URL."""

    utm_term: OptionalNullable[str] = UNSET
    r"""The UTM term of the short link. If set, this will populate or override the UTM term in the destination URL."""

    utm_content: OptionalNullable[str] = UNSET
    r"""The UTM content of the short link. If set, this will populate or override the UTM content in the destination URL."""

    ref: OptionalNullable[str] = UNSET
    r"""The referral tag of the short link. If set, this will populate or override the `ref` query parameter in the destination URL."""

    webhook_ids: Annotated[
        OptionalNullable[List[str]], pydantic.Field(alias="webhookIds")
    ] = UNSET
    r"""An array of webhook IDs to trigger when the link is clicked. These webhooks will receive click event data."""

    test_variants: Annotated[
        OptionalNullable[List[UpsertLinkTestVariants]],
        pydantic.Field(alias="testVariants"),
    ] = UNSET
    r"""An array of A/B test URLs and the percentage of traffic to send to each URL."""

    test_started_at: Annotated[
        OptionalNullable[str], pydantic.Field(alias="testStartedAt")
    ] = UNSET
    r"""The date and time when the tests started."""

    test_completed_at: Annotated[
        OptionalNullable[str], pydantic.Field(alias="testCompletedAt")
    ] = UNSET
    r"""The date and time when the tests were or will be completed."""

    public_stats: Annotated[
        Optional[bool],
        pydantic.Field(
            deprecated="warning: ** DEPRECATED ** - This will be removed in a future release, please migrate away from it as soon as possible.",
            alias="publicStats",
        ),
    ] = None
    r"""Deprecated: Use `dashboard` instead. Whether the short link's stats are publicly accessible. Defaults to `false` if not provided."""

    tag_id: Annotated[
        OptionalNullable[str],
        pydantic.Field(
            deprecated="warning: ** DEPRECATED ** - This will be removed in a future release, please migrate away from it as soon as possible.",
            alias="tagId",
        ),
    ] = UNSET
    r"""Deprecated: Use `tagIds` instead. The unique ID of the tag assigned to the short link."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "domain",
                "key",
                "keyLength",
                "externalId",
                "tenantId",
                "programId",
                "partnerId",
                "prefix",
                "trackConversion",
                "archived",
                "tagIds",
                "tagNames",
                "folderId",
                "comments",
                "expiresAt",
                "expiredUrl",
                "password",
                "proxy",
                "title",
                "description",
                "image",
                "video",
                "rewrite",
                "ios",
                "android",
                "geo",
                "doIndex",
                "utm_source",
                "utm_medium",
                "utm_campaign",
                "utm_term",
                "utm_content",
                "ref",
                "webhookIds",
                "testVariants",
                "testStartedAt",
                "testCompletedAt",
                "publicStats",
                "tagId",
            ]
        )
        nullable_fields = set(
            [
                "externalId",
                "tenantId",
                "programId",
                "partnerId",
                "folderId",
                "comments",
                "expiresAt",
                "expiredUrl",
                "password",
                "title",
                "description",
                "image",
                "video",
                "ios",
                "android",
                "geo",
                "utm_source",
                "utm_medium",
                "utm_campaign",
                "utm_term",
                "utm_content",
                "ref",
                "webhookIds",
                "testVariants",
                "testStartedAt",
                "testCompletedAt",
                "tagId",
            ]
        )
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

            if val != UNSET_SENTINEL:
                if (
                    val is not None
                    or k not in optional_fields
                    or is_nullable_and_explicitly_set
                ):
                    m[k] = val

        return m
