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

from __future__ import annotations
from codat_bankfeeds.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from datetime import datetime
import pydantic
from pydantic import model_serializer
from typing import List
from typing_extensions import Annotated, NotRequired, TypedDict


class SyncStatusResultTypedDict(TypedDict):
    r"""Represents the status of a managed bank feed sync."""

    client_id: str
    r"""The ID of the client associated with the company that owns the source bank account."""
    company_id: str
    r"""The ID of the company that owns the source bank account."""
    company_name: str
    r"""The name of the company that owns the source bank account."""
    data_connection_id: str
    r"""The ID of the target accounting data connection."""
    execution_start_utc: datetime
    r"""The UTC timestamp when the sync started executing."""
    period_end_utc: datetime
    r"""The end of the sync period."""
    period_start_utc: datetime
    r"""The start of the sync period."""
    source_account_id: str
    r"""The ID of the source bank account from which transactions are fetched."""
    status: str
    r"""The current status of the sync. Known values are `Started`, `Failed`, `SuccessDataPushed`, `SuccessNoDataPushed`, `PartialSuccessDataPushed`."""
    sync_id: str
    r"""The unique identifier for the sync."""
    error_code: NotRequired[Nullable[str]]
    r"""Error code if the sync failed."""
    error_message: NotRequired[Nullable[str]]
    r"""Error message if the sync failed."""
    execution_end_utc: NotRequired[Nullable[datetime]]
    r"""The UTC timestamp when the sync execution ended, if completed (i.e. `status` is any value other than `Started`)."""
    push_operation_keys: NotRequired[Nullable[List[str]]]
    r"""List of write operation keys associated with the sync, if the sync resulted in write attempts."""


class SyncStatusResult(BaseModel):
    r"""Represents the status of a managed bank feed sync."""

    client_id: Annotated[str, pydantic.Field(alias="clientId")]
    r"""The ID of the client associated with the company that owns the source bank account."""

    company_id: Annotated[str, pydantic.Field(alias="companyId")]
    r"""The ID of the company that owns the source bank account."""

    company_name: Annotated[str, pydantic.Field(alias="companyName")]
    r"""The name of the company that owns the source bank account."""

    data_connection_id: Annotated[str, pydantic.Field(alias="dataConnectionId")]
    r"""The ID of the target accounting data connection."""

    execution_start_utc: Annotated[datetime, pydantic.Field(alias="executionStartUtc")]
    r"""The UTC timestamp when the sync started executing."""

    period_end_utc: Annotated[datetime, pydantic.Field(alias="periodEndUtc")]
    r"""The end of the sync period."""

    period_start_utc: Annotated[datetime, pydantic.Field(alias="periodStartUtc")]
    r"""The start of the sync period."""

    source_account_id: Annotated[str, pydantic.Field(alias="sourceAccountId")]
    r"""The ID of the source bank account from which transactions are fetched."""

    status: str
    r"""The current status of the sync. Known values are `Started`, `Failed`, `SuccessDataPushed`, `SuccessNoDataPushed`, `PartialSuccessDataPushed`."""

    sync_id: Annotated[str, pydantic.Field(alias="syncId")]
    r"""The unique identifier for the sync."""

    error_code: Annotated[OptionalNullable[str], pydantic.Field(alias="errorCode")] = (
        UNSET
    )
    r"""Error code if the sync failed."""

    error_message: Annotated[
        OptionalNullable[str], pydantic.Field(alias="errorMessage")
    ] = UNSET
    r"""Error message if the sync failed."""

    execution_end_utc: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="executionEndUtc")
    ] = UNSET
    r"""The UTC timestamp when the sync execution ended, if completed (i.e. `status` is any value other than `Started`)."""

    push_operation_keys: Annotated[
        OptionalNullable[List[str]], pydantic.Field(alias="pushOperationKeys")
    ] = UNSET
    r"""List of write operation keys associated with the sync, if the sync resulted in write attempts."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["errorCode", "errorMessage", "executionEndUtc", "pushOperationKeys"]
        )
        nullable_fields = set(
            ["errorCode", "errorMessage", "executionEndUtc", "pushOperationKeys"]
        )
        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
