# coding: utf-8

"""
    Core Operations

    The Sparkfly Platform provides a full lifecycle for promotions and rewards from creation to distribution to settlement. The platform integrates in real-time at the point-of-sale and provides item level discounting and tracking. The capabilities of the Sparkfly Platform are available through the use of the Sparkfly Platform API.  The Sparkfly documentation site is under development. If the documentation you're after isn't available here, please contact support@sparkfly.com and we will get you what you need.

    The version of the OpenAPI document: 1.0
    Contact: support@sparkfly.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from datetime import datetime
from pydantic import BaseModel, ConfigDict, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from sparkfly.models.transaction_applied_credential import TransactionAppliedCredential
from sparkfly.models.transaction_item import TransactionItem
from sparkfly.models.transaction_redemption import TransactionRedemption
from sparkfly.models.transaction_totals import TransactionTotals
from typing import Optional, Set
from typing_extensions import Self

class TransactionResponseTransaction(BaseModel):
    """
    TransactionResponseTransaction
    """ # noqa: E501
    id: Optional[StrictInt] = None
    transaction_identifier: Optional[StrictStr] = None
    receipt_id: Optional[StrictStr] = None
    totals: Optional[TransactionTotals] = None
    external_id: Optional[StrictStr] = None
    store_number: Optional[StrictStr] = None
    store_id: Optional[StrictInt] = None
    occurred_at: Optional[datetime] = None
    transaction_items: Optional[List[TransactionItem]] = None
    redemptions: Optional[List[TransactionRedemption]] = None
    applied_credentials: Optional[List[TransactionAppliedCredential]] = None
    __properties: ClassVar[List[str]] = ["id", "transaction_identifier", "receipt_id", "totals", "external_id", "store_number", "store_id", "occurred_at", "transaction_items", "redemptions", "applied_credentials"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of TransactionResponseTransaction from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of totals
        if self.totals:
            _dict['totals'] = self.totals.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in transaction_items (list)
        _items = []
        if self.transaction_items:
            for _item_transaction_items in self.transaction_items:
                if _item_transaction_items:
                    _items.append(_item_transaction_items.to_dict())
            _dict['transaction_items'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in redemptions (list)
        _items = []
        if self.redemptions:
            for _item_redemptions in self.redemptions:
                if _item_redemptions:
                    _items.append(_item_redemptions.to_dict())
            _dict['redemptions'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in applied_credentials (list)
        _items = []
        if self.applied_credentials:
            for _item_applied_credentials in self.applied_credentials:
                if _item_applied_credentials:
                    _items.append(_item_applied_credentials.to_dict())
            _dict['applied_credentials'] = _items
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of TransactionResponseTransaction from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "id": obj.get("id"),
            "transaction_identifier": obj.get("transaction_identifier"),
            "receipt_id": obj.get("receipt_id"),
            "totals": TransactionTotals.from_dict(obj["totals"]) if obj.get("totals") is not None else None,
            "external_id": obj.get("external_id"),
            "store_number": obj.get("store_number"),
            "store_id": obj.get("store_id"),
            "occurred_at": obj.get("occurred_at"),
            "transaction_items": [TransactionItem.from_dict(_item) for _item in obj["transaction_items"]] if obj.get("transaction_items") is not None else None,
            "redemptions": [TransactionRedemption.from_dict(_item) for _item in obj["redemptions"]] if obj.get("redemptions") is not None else None,
            "applied_credentials": [TransactionAppliedCredential.from_dict(_item) for _item in obj["applied_credentials"]] if obj.get("applied_credentials") is not None else None
        })
        return _obj


