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

from __future__ import annotations
from .articlebyline import ArticleByline
from .articlevisibility import ArticleVisibility
from datetime import datetime
from polar_sdk.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from pydantic import model_serializer
from typing_extensions import NotRequired, TypedDict


class ArticleUpdateTypedDict(TypedDict):
    title: NotRequired[Nullable[str]]
    body: NotRequired[Nullable[str]]
    r"""Body in string format. body and body_base64 are mutually exclusive."""
    body_base64: NotRequired[Nullable[str]]
    r"""Body in base64-encoded format. Can be helpful to bypass Web Application Firewalls (WAF). body and body_base64 are mutually exclusive."""
    slug: NotRequired[Nullable[str]]
    byline: NotRequired[Nullable[ArticleByline]]
    r"""If the user or organization should be credited in the byline."""
    visibility: NotRequired[Nullable[ArticleVisibility]]
    paid_subscribers_only: NotRequired[Nullable[bool]]
    r"""Set to true to only make this article available for subscribers to a paid subscription tier in the organization."""
    paid_subscribers_only_ends_at: NotRequired[Nullable[datetime]]
    r"""If specified, time at which the article should no longer be restricted to paid subscribers. Only relevant if `paid_subscribers_only` is true."""
    published_at: NotRequired[Nullable[datetime]]
    r"""Time of publishing. If this date is in the future, the post will be scheduled to publish at this time."""
    notify_subscribers: NotRequired[Nullable[bool]]
    r"""Set to true to deliver this article via email and/or notifications to subscribers."""
    is_pinned: NotRequired[Nullable[bool]]
    r"""If the article should be pinned"""
    og_image_url: NotRequired[Nullable[str]]
    r"""Custom og:image URL value"""
    og_description: NotRequired[Nullable[str]]
    r"""Custom og:description value"""


class ArticleUpdate(BaseModel):
    title: OptionalNullable[str] = UNSET

    body: OptionalNullable[str] = UNSET
    r"""Body in string format. body and body_base64 are mutually exclusive."""

    body_base64: OptionalNullable[str] = UNSET
    r"""Body in base64-encoded format. Can be helpful to bypass Web Application Firewalls (WAF). body and body_base64 are mutually exclusive."""

    slug: OptionalNullable[str] = UNSET

    byline: OptionalNullable[ArticleByline] = UNSET
    r"""If the user or organization should be credited in the byline."""

    visibility: OptionalNullable[ArticleVisibility] = UNSET

    paid_subscribers_only: OptionalNullable[bool] = UNSET
    r"""Set to true to only make this article available for subscribers to a paid subscription tier in the organization."""

    paid_subscribers_only_ends_at: OptionalNullable[datetime] = UNSET
    r"""If specified, time at which the article should no longer be restricted to paid subscribers. Only relevant if `paid_subscribers_only` is true."""

    published_at: OptionalNullable[datetime] = UNSET
    r"""Time of publishing. If this date is in the future, the post will be scheduled to publish at this time."""

    notify_subscribers: OptionalNullable[bool] = UNSET
    r"""Set to true to deliver this article via email and/or notifications to subscribers."""

    is_pinned: OptionalNullable[bool] = UNSET
    r"""If the article should be pinned"""

    og_image_url: OptionalNullable[str] = UNSET
    r"""Custom og:image URL value"""

    og_description: OptionalNullable[str] = UNSET
    r"""Custom og:description value"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "title",
            "body",
            "body_base64",
            "slug",
            "byline",
            "visibility",
            "paid_subscribers_only",
            "paid_subscribers_only_ends_at",
            "published_at",
            "notify_subscribers",
            "is_pinned",
            "og_image_url",
            "og_description",
        ]
        nullable_fields = [
            "title",
            "body",
            "body_base64",
            "slug",
            "byline",
            "visibility",
            "paid_subscribers_only",
            "paid_subscribers_only_ends_at",
            "published_at",
            "notify_subscribers",
            "is_pinned",
            "og_image_url",
            "og_description",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in 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
