# coding: utf-8

"""
    Coinbase Developer Platform APIs

    The Coinbase Developer Platform APIs - leading the world's transition onchain.

    The version of the OpenAPI document: 2.0.0
    Contact: cdp@coinbase.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

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


from __future__ import annotations
import json
import pprint
from pydantic import BaseModel, ConfigDict, Field, StrictStr, ValidationError, field_validator
from typing import Any, List, Optional
from cdp.openapi_client.models.prepare_user_operation_rule import PrepareUserOperationRule
from cdp.openapi_client.models.send_evm_transaction_rule import SendEvmTransactionRule
from cdp.openapi_client.models.send_sol_transaction_rule import SendSolTransactionRule
from cdp.openapi_client.models.send_user_operation_rule import SendUserOperationRule
from cdp.openapi_client.models.sign_evm_hash_rule import SignEvmHashRule
from cdp.openapi_client.models.sign_evm_message_rule import SignEvmMessageRule
from cdp.openapi_client.models.sign_evm_transaction_rule import SignEvmTransactionRule
from cdp.openapi_client.models.sign_evm_typed_data_rule import SignEvmTypedDataRule
from cdp.openapi_client.models.sign_sol_message_rule import SignSolMessageRule
from cdp.openapi_client.models.sign_sol_transaction_rule import SignSolTransactionRule
from pydantic import StrictStr, Field
from typing import Union, List, Set, Optional, Dict
from typing_extensions import Literal, Self

RULE_ONE_OF_SCHEMAS = ["PrepareUserOperationRule", "SendEvmTransactionRule", "SendSolTransactionRule", "SendUserOperationRule", "SignEvmHashRule", "SignEvmMessageRule", "SignEvmTransactionRule", "SignEvmTypedDataRule", "SignSolMessageRule", "SignSolTransactionRule"]

class Rule(BaseModel):
    """
    A rule that limits the behavior of an account.
    """
    # data type: SignEvmTransactionRule
    oneof_schema_1_validator: Optional[SignEvmTransactionRule] = None
    # data type: SendEvmTransactionRule
    oneof_schema_2_validator: Optional[SendEvmTransactionRule] = None
    # data type: SignEvmMessageRule
    oneof_schema_3_validator: Optional[SignEvmMessageRule] = None
    # data type: SignEvmTypedDataRule
    oneof_schema_4_validator: Optional[SignEvmTypedDataRule] = None
    # data type: SignSolTransactionRule
    oneof_schema_5_validator: Optional[SignSolTransactionRule] = None
    # data type: SendSolTransactionRule
    oneof_schema_6_validator: Optional[SendSolTransactionRule] = None
    # data type: SignSolMessageRule
    oneof_schema_7_validator: Optional[SignSolMessageRule] = None
    # data type: SignEvmHashRule
    oneof_schema_8_validator: Optional[SignEvmHashRule] = None
    # data type: PrepareUserOperationRule
    oneof_schema_9_validator: Optional[PrepareUserOperationRule] = None
    # data type: SendUserOperationRule
    oneof_schema_10_validator: Optional[SendUserOperationRule] = None
    actual_instance: Optional[Union[PrepareUserOperationRule, SendEvmTransactionRule, SendSolTransactionRule, SendUserOperationRule, SignEvmHashRule, SignEvmMessageRule, SignEvmTransactionRule, SignEvmTypedDataRule, SignSolMessageRule, SignSolTransactionRule]] = None
    one_of_schemas: Set[str] = { "PrepareUserOperationRule", "SendEvmTransactionRule", "SendSolTransactionRule", "SendUserOperationRule", "SignEvmHashRule", "SignEvmMessageRule", "SignEvmTransactionRule", "SignEvmTypedDataRule", "SignSolMessageRule", "SignSolTransactionRule" }

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


    def __init__(self, *args, **kwargs) -> None:
        if args:
            if len(args) > 1:
                raise ValueError("If a position argument is used, only 1 is allowed to set `actual_instance`")
            if kwargs:
                raise ValueError("If a position argument is used, keyword arguments cannot be used.")
            super().__init__(actual_instance=args[0])
        else:
            super().__init__(**kwargs)

    @field_validator('actual_instance')
    def actual_instance_must_validate_oneof(cls, v):
        instance = Rule.model_construct()
        error_messages = []
        match = 0
        # validate data type: SignEvmTransactionRule
        if not isinstance(v, SignEvmTransactionRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SignEvmTransactionRule`")
        else:
            match += 1
        # validate data type: SendEvmTransactionRule
        if not isinstance(v, SendEvmTransactionRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SendEvmTransactionRule`")
        else:
            match += 1
        # validate data type: SignEvmMessageRule
        if not isinstance(v, SignEvmMessageRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SignEvmMessageRule`")
        else:
            match += 1
        # validate data type: SignEvmTypedDataRule
        if not isinstance(v, SignEvmTypedDataRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SignEvmTypedDataRule`")
        else:
            match += 1
        # validate data type: SignSolTransactionRule
        if not isinstance(v, SignSolTransactionRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SignSolTransactionRule`")
        else:
            match += 1
        # validate data type: SendSolTransactionRule
        if not isinstance(v, SendSolTransactionRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SendSolTransactionRule`")
        else:
            match += 1
        # validate data type: SignSolMessageRule
        if not isinstance(v, SignSolMessageRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SignSolMessageRule`")
        else:
            match += 1
        # validate data type: SignEvmHashRule
        if not isinstance(v, SignEvmHashRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SignEvmHashRule`")
        else:
            match += 1
        # validate data type: PrepareUserOperationRule
        if not isinstance(v, PrepareUserOperationRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `PrepareUserOperationRule`")
        else:
            match += 1
        # validate data type: SendUserOperationRule
        if not isinstance(v, SendUserOperationRule):
            error_messages.append(f"Error! Input type `{type(v)}` is not `SendUserOperationRule`")
        else:
            match += 1
        if match > 1:
            # more than 1 match
            raise ValueError("Multiple matches found when setting `actual_instance` in Rule with oneOf schemas: PrepareUserOperationRule, SendEvmTransactionRule, SendSolTransactionRule, SendUserOperationRule, SignEvmHashRule, SignEvmMessageRule, SignEvmTransactionRule, SignEvmTypedDataRule, SignSolMessageRule, SignSolTransactionRule. Details: " + ", ".join(error_messages))
        elif match == 0:
            # no match
            raise ValueError("No match found when setting `actual_instance` in Rule with oneOf schemas: PrepareUserOperationRule, SendEvmTransactionRule, SendSolTransactionRule, SendUserOperationRule, SignEvmHashRule, SignEvmMessageRule, SignEvmTransactionRule, SignEvmTypedDataRule, SignSolMessageRule, SignSolTransactionRule. Details: " + ", ".join(error_messages))
        else:
            return v

    @classmethod
    def from_dict(cls, obj: Union[str, Dict[str, Any]]) -> Self:
        return cls.from_json(json.dumps(obj))

    @classmethod
    def from_json(cls, json_str: str) -> Self:
        """Returns the object represented by the json string"""
        instance = cls.model_construct()
        error_messages = []
        match = 0

        # deserialize data into SignEvmTransactionRule
        try:
            instance.actual_instance = SignEvmTransactionRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into SendEvmTransactionRule
        try:
            instance.actual_instance = SendEvmTransactionRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into SignEvmMessageRule
        try:
            instance.actual_instance = SignEvmMessageRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into SignEvmTypedDataRule
        try:
            instance.actual_instance = SignEvmTypedDataRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into SignSolTransactionRule
        try:
            instance.actual_instance = SignSolTransactionRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into SendSolTransactionRule
        try:
            instance.actual_instance = SendSolTransactionRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into SignSolMessageRule
        try:
            instance.actual_instance = SignSolMessageRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into SignEvmHashRule
        try:
            instance.actual_instance = SignEvmHashRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into PrepareUserOperationRule
        try:
            instance.actual_instance = PrepareUserOperationRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))
        # deserialize data into SendUserOperationRule
        try:
            instance.actual_instance = SendUserOperationRule.from_json(json_str)
            match += 1
        except (ValidationError, ValueError) as e:
            error_messages.append(str(e))

        if match > 1:
            # more than 1 match
            raise ValueError("Multiple matches found when deserializing the JSON string into Rule with oneOf schemas: PrepareUserOperationRule, SendEvmTransactionRule, SendSolTransactionRule, SendUserOperationRule, SignEvmHashRule, SignEvmMessageRule, SignEvmTransactionRule, SignEvmTypedDataRule, SignSolMessageRule, SignSolTransactionRule. Details: " + ", ".join(error_messages))
        elif match == 0:
            # no match
            raise ValueError("No match found when deserializing the JSON string into Rule with oneOf schemas: PrepareUserOperationRule, SendEvmTransactionRule, SendSolTransactionRule, SendUserOperationRule, SignEvmHashRule, SignEvmMessageRule, SignEvmTransactionRule, SignEvmTypedDataRule, SignSolMessageRule, SignSolTransactionRule. Details: " + ", ".join(error_messages))
        else:
            return instance

    def to_json(self) -> str:
        """Returns the JSON representation of the actual instance"""
        if self.actual_instance is None:
            return "null"

        if hasattr(self.actual_instance, "to_json") and callable(self.actual_instance.to_json):
            return self.actual_instance.to_json()
        else:
            return json.dumps(self.actual_instance)

    def to_dict(self) -> Optional[Union[Dict[str, Any], PrepareUserOperationRule, SendEvmTransactionRule, SendSolTransactionRule, SendUserOperationRule, SignEvmHashRule, SignEvmMessageRule, SignEvmTransactionRule, SignEvmTypedDataRule, SignSolMessageRule, SignSolTransactionRule]]:
        """Returns the dict representation of the actual instance"""
        if self.actual_instance is None:
            return None

        if hasattr(self.actual_instance, "to_dict") and callable(self.actual_instance.to_dict):
            return self.actual_instance.to_dict()
        else:
            # primitive type
            return self.actual_instance

    def to_str(self) -> str:
        """Returns the string representation of the actual instance"""
        return pprint.pformat(self.model_dump())


