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

from __future__ import annotations
from .custommappings import CustomMappings, CustomMappingsTypedDict
from apideck_unify.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import Any, Optional
from typing_extensions import NotRequired, TypedDict


class IncomeTypedDict(TypedDict):
    total: Nullable[float]
    r"""Total income"""
    records: Any


class Income(BaseModel):
    total: Nullable[float]
    r"""Total income"""

    records: Any

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


class ExpensesModelTypedDict(TypedDict):
    total: Nullable[float]
    r"""Total expense"""
    records: Any


class ExpensesModel(BaseModel):
    total: Nullable[float]
    r"""Total expense"""

    records: Any

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


class NetIncomeTypedDict(TypedDict):
    total: Nullable[float]
    r"""Total net income"""
    records: Any


class NetIncome(BaseModel):
    total: Nullable[float]
    r"""Total net income"""

    records: Any

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


class NetOperatingIncomeTypedDict(TypedDict):
    total: Nullable[float]
    r"""Total net operating income"""
    records: Any


class NetOperatingIncome(BaseModel):
    total: Nullable[float]
    r"""Total net operating income"""

    records: Any

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


class GrossProfitTypedDict(TypedDict):
    total: Nullable[float]
    r"""Total gross profit"""
    records: Any


class GrossProfit(BaseModel):
    total: Nullable[float]
    r"""Total gross profit"""

    records: Any

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


class ProfitAndLossTypedDict(TypedDict):
    report_name: str
    r"""The name of the report"""
    currency: str
    income: IncomeTypedDict
    expenses: ExpensesModelTypedDict
    id: NotRequired[str]
    r"""A unique identifier for an object."""
    start_date: NotRequired[str]
    r"""The start date of the report"""
    end_date: NotRequired[str]
    r"""The start date of the report"""
    customer_id: NotRequired[str]
    r"""Customer id"""
    net_income: NotRequired[Nullable[NetIncomeTypedDict]]
    net_operating_income: NotRequired[Nullable[NetOperatingIncomeTypedDict]]
    gross_profit: NotRequired[Nullable[GrossProfitTypedDict]]
    custom_mappings: NotRequired[Nullable[CustomMappingsTypedDict]]
    r"""When custom mappings are configured on the resource, the result is included here."""


class ProfitAndLoss(BaseModel):
    report_name: str
    r"""The name of the report"""

    currency: str

    income: Income

    expenses: ExpensesModel

    id: Optional[str] = None
    r"""A unique identifier for an object."""

    start_date: Optional[str] = None
    r"""The start date of the report"""

    end_date: Optional[str] = None
    r"""The start date of the report"""

    customer_id: Optional[str] = None
    r"""Customer id"""

    net_income: OptionalNullable[NetIncome] = UNSET

    net_operating_income: OptionalNullable[NetOperatingIncome] = UNSET

    gross_profit: OptionalNullable[GrossProfit] = UNSET

    custom_mappings: OptionalNullable[CustomMappings] = UNSET
    r"""When custom mappings are configured on the resource, the result is included here."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "id",
            "start_date",
            "end_date",
            "customer_id",
            "net_income",
            "net_operating_income",
            "gross_profit",
            "custom_mappings",
        ]
        nullable_fields = [
            "net_income",
            "net_operating_income",
            "gross_profit",
            "custom_mappings",
        ]
        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
