# coding: utf-8

"""
    Verity471 Unified API

      - creds: 1.0.1
  - indicators: 1.0.0
  - malware: 1.0.1
  - reports: 1.0.1
  - sources: 1.0.3
  - actors: 1.0.2
  - watchers: 1.0.0
  - observables: 1.0.0
  - entities: 1.0.0
  - girs: 1.0.0


    The version of the OpenAPI document: 1.1.3
    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 pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from verity471.models.cred_credential_set_response import CredCredentialSetResponse
from verity471.models.cred_password_response import CredPasswordResponse
from verity471.models.info_stealer_response_set import InfoStealerResponseSet
from typing import Optional, Set, TYPE_CHECKING
from typing_extensions import Self

if TYPE_CHECKING:
    # Only for type checkers; does not run at runtime.
    from verity471.verity_stix import STIXMapperSettings


class CredDataResponse(BaseModel):
    """
    Credential with related objects.
    """ # noqa: E501
    credential_login: Optional[StrictStr] = Field(default=None, description="Login of the credential.")
    credential_domain: Optional[StrictStr] = Field(default=None, description="Domain of the credential.")
    detection_domain: Optional[StrictStr] = Field(default=None, description="Detection domain of the credential.")
    affiliations: Optional[List[StrictStr]] = Field(default=None, description="Affiliation of the credential. Allowed values: my_employees, vip_emails, my_customers, third_parties.")
    password: Optional[CredPasswordResponse] = None
    credential_sets: Optional[List[CredCredentialSetResponse]] = Field(default=None, description="Credential sets associated with the credential.")
    info_stealer: InfoStealerResponseSet
    accessed_url: Optional[StrictStr] = Field(default=None, description="Accessed URL of the credential.")
    credential_set_type: Optional[List[StrictStr]] = Field(default=None, description="Type(s) of the credential set(s) this credential belongs to.")
    __properties: ClassVar[List[str]] = ["credential_login", "credential_domain", "detection_domain", "affiliations", "password", "credential_sets", "info_stealer", "accessed_url", "credential_set_type"]

    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_stix(self, settings: Optional["STIXMapperSettings"] = None):
        try:
            from verity471.verity_stix.mappers.common import StixMapper
        except ImportError as e:
            raise ImportError(
                "STIX support is an optional feature. "
                "Install with: pip install 'verity471[stix]'"
            ) from e
        stix_mapper = StixMapper(settings)
        return stix_mapper.map(self.to_dict())

    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 CredDataResponse 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 password
        if self.password:
            _dict['password'] = self.password.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in credential_sets (list)
        _items = []
        if self.credential_sets:
            for _item_credential_sets in self.credential_sets:
                if _item_credential_sets:
                    _items.append(_item_credential_sets.to_dict())
            _dict['credential_sets'] = _items
        # override the default output from pydantic by calling `to_dict()` of info_stealer
        if self.info_stealer:
            _dict['info_stealer'] = self.info_stealer.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "credential_login": obj.get("credential_login"),
            "credential_domain": obj.get("credential_domain"),
            "detection_domain": obj.get("detection_domain"),
            "affiliations": obj.get("affiliations"),
            "password": CredPasswordResponse.from_dict(obj["password"]) if obj.get("password") is not None else None,
            "credential_sets": [CredCredentialSetResponse.from_dict(_item) for _item in obj["credential_sets"]] if obj.get("credential_sets") is not None else None,
            "info_stealer": InfoStealerResponseSet.from_dict(obj["info_stealer"]) if obj.get("info_stealer") is not None else None,
            "accessed_url": obj.get("accessed_url"),
            "credential_set_type": obj.get("credential_set_type")
        })
        return _obj


