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

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


class SourceRuleTypedDict(TypedDict):
    r"""An auto-acceptance rule that automatically approves source events matching specified conditions. Rules help automate the approval process for trusted customers or specific transaction patterns."""

    id: str
    r"""Unique identifier for the rule"""
    conditions: List[RuleConditionTypedDict]
    r"""List of conditions that must ALL be met (AND logic)"""
    created_at: datetime
    r"""When the rule was created"""
    description: str
    r"""Detailed description of what this source rule does"""
    enabled: bool
    r"""Whether this rule is currently active"""
    evaluation_count: int
    r"""Total number of times this rule has been evaluated"""
    match_count: int
    r"""Number of times this rule has matched and auto-accepted an invoice"""
    name: str
    r"""Human-readable name for the source rule"""
    order: int
    r"""Priority order for rule evaluation (lower numbers are evaluated first)"""
    source_id: str
    r"""ID of the source this rule belongs to"""
    updated_at: datetime
    r"""When the rule was last updated"""
    created_by: NotRequired[Nullable[str]]
    r"""User ID who created the rule"""
    last_evaluated_at: NotRequired[Nullable[datetime]]
    r"""Last time this rule was evaluated against an invoice"""


class SourceRule(BaseModel):
    r"""An auto-acceptance rule that automatically approves source events matching specified conditions. Rules help automate the approval process for trusted customers or specific transaction patterns."""

    id: str
    r"""Unique identifier for the rule"""

    conditions: List[RuleCondition]
    r"""List of conditions that must ALL be met (AND logic)"""

    created_at: Annotated[datetime, pydantic.Field(alias="createdAt")]
    r"""When the rule was created"""

    description: str
    r"""Detailed description of what this source rule does"""

    enabled: bool
    r"""Whether this rule is currently active"""

    evaluation_count: Annotated[int, pydantic.Field(alias="evaluationCount")]
    r"""Total number of times this rule has been evaluated"""

    match_count: Annotated[int, pydantic.Field(alias="matchCount")]
    r"""Number of times this rule has matched and auto-accepted an invoice"""

    name: str
    r"""Human-readable name for the source rule"""

    order: int
    r"""Priority order for rule evaluation (lower numbers are evaluated first)"""

    source_id: Annotated[str, pydantic.Field(alias="sourceId")]
    r"""ID of the source this rule belongs to"""

    updated_at: Annotated[datetime, pydantic.Field(alias="updatedAt")]
    r"""When the rule was last updated"""

    created_by: Annotated[OptionalNullable[str], pydantic.Field(alias="createdBy")] = (
        UNSET
    )
    r"""User ID who created the rule"""

    last_evaluated_at: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="lastEvaluatedAt")
    ] = UNSET
    r"""Last time this rule was evaluated against an invoice"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["createdBy", "lastEvaluatedAt"])
        nullable_fields = set(["createdBy", "lastEvaluatedAt"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))
            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


try:
    SourceRule.model_rebuild()
except NameError:
    pass
