# The Okta software accompanied by this notice is provided pursuant to the following terms:
# Copyright © 2025-Present, Okta, Inc.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
# License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
# IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
# coding: utf-8

"""
Okta Admin Management

Allows customers to easily access the Okta Management APIs

The version of the OpenAPI document: 5.1.0
Contact: devex-public@okta.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
import re  # noqa: F401
from datetime import datetime
from typing import Any, ClassVar, Dict, List
from typing import Optional, Set

from pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing_extensions import Self

from okta.models.log_actor import LogActor
from okta.models.log_authentication_context import LogAuthenticationContext
from okta.models.log_client import LogClient
from okta.models.log_debug_context import LogDebugContext
from okta.models.log_outcome import LogOutcome
from okta.models.log_request import LogRequest
from okta.models.log_security_context import LogSecurityContext
from okta.models.log_severity import LogSeverity
from okta.models.log_target import LogTarget
from okta.models.log_transaction import LogTransaction


class LogEvent(BaseModel):
    """
    LogEvent
    """  # noqa: E501

    actor: Optional[LogActor] = None
    authentication_context: Optional[LogAuthenticationContext] = Field(
        default=None, alias="authenticationContext"
    )
    client: Optional[LogClient] = None
    debug_context: Optional[LogDebugContext] = Field(default=None, alias="debugContext")
    display_message: Optional[StrictStr] = Field(default=None, alias="displayMessage")
    event_type: Optional[StrictStr] = Field(default=None, alias="eventType")
    legacy_event_type: Optional[StrictStr] = Field(
        default=None, alias="legacyEventType"
    )
    outcome: Optional[LogOutcome] = None
    published: Optional[datetime] = None
    request: Optional[LogRequest] = None
    security_context: Optional[LogSecurityContext] = Field(
        default=None, alias="securityContext"
    )
    severity: Optional[LogSeverity] = None
    target: Optional[List[LogTarget]] = None
    transaction: Optional[LogTransaction] = None
    uuid: Optional[StrictStr] = None
    version: Optional[StrictStr] = None
    __properties: ClassVar[List[str]] = [
        "actor",
        "authenticationContext",
        "client",
        "debugContext",
        "displayMessage",
        "eventType",
        "legacyEventType",
        "outcome",
        "published",
        "request",
        "securityContext",
        "severity",
        "target",
        "transaction",
        "uuid",
        "version",
    ]

    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 LogEvent 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.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        """
        excluded_fields: Set[str] = set(
            [
                "display_message",
                "event_type",
                "legacy_event_type",
                "published",
                "target",
                "uuid",
                "version",
            ]
        )

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of actor
        if self.actor:
            if not isinstance(self.actor, dict):
                _dict["actor"] = self.actor.to_dict()
            else:
                _dict["actor"] = self.actor

        # override the default output from pydantic by calling `to_dict()` of authentication_context
        if self.authentication_context:
            if not isinstance(self.authentication_context, dict):
                _dict["authenticationContext"] = self.authentication_context.to_dict()
            else:
                _dict["authenticationContext"] = self.authentication_context

        # override the default output from pydantic by calling `to_dict()` of client
        if self.client:
            if not isinstance(self.client, dict):
                _dict["client"] = self.client.to_dict()
            else:
                _dict["client"] = self.client

        # override the default output from pydantic by calling `to_dict()` of debug_context
        if self.debug_context:
            if not isinstance(self.debug_context, dict):
                _dict["debugContext"] = self.debug_context.to_dict()
            else:
                _dict["debugContext"] = self.debug_context

        # override the default output from pydantic by calling `to_dict()` of outcome
        if self.outcome:
            if not isinstance(self.outcome, dict):
                _dict["outcome"] = self.outcome.to_dict()
            else:
                _dict["outcome"] = self.outcome

        # override the default output from pydantic by calling `to_dict()` of request
        if self.request:
            if not isinstance(self.request, dict):
                _dict["request"] = self.request.to_dict()
            else:
                _dict["request"] = self.request

        # override the default output from pydantic by calling `to_dict()` of security_context
        if self.security_context:
            if not isinstance(self.security_context, dict):
                _dict["securityContext"] = self.security_context.to_dict()
            else:
                _dict["securityContext"] = self.security_context

        # override the default output from pydantic by calling `to_dict()` of each item in target (list)
        _items = []
        if self.target:
            for _item in self.target:
                if _item:
                    _items.append(_item.to_dict())
            _dict["target"] = _items
        # override the default output from pydantic by calling `to_dict()` of transaction
        if self.transaction:
            if not isinstance(self.transaction, dict):
                _dict["transaction"] = self.transaction.to_dict()
            else:
                _dict["transaction"] = self.transaction

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "actor": (
                    LogActor.from_dict(obj["actor"])
                    if obj.get("actor") is not None
                    else None
                ),
                "authenticationContext": (
                    LogAuthenticationContext.from_dict(obj["authenticationContext"])
                    if obj.get("authenticationContext") is not None
                    else None
                ),
                "client": (
                    LogClient.from_dict(obj["client"])
                    if obj.get("client") is not None
                    else None
                ),
                "debugContext": (
                    LogDebugContext.from_dict(obj["debugContext"])
                    if obj.get("debugContext") is not None
                    else None
                ),
                "displayMessage": obj.get("displayMessage"),
                "eventType": obj.get("eventType"),
                "legacyEventType": obj.get("legacyEventType"),
                "outcome": (
                    LogOutcome.from_dict(obj["outcome"])
                    if obj.get("outcome") is not None
                    else None
                ),
                "published": obj.get("published"),
                "request": (
                    LogRequest.from_dict(obj["request"])
                    if obj.get("request") is not None
                    else None
                ),
                "securityContext": (
                    LogSecurityContext.from_dict(obj["securityContext"])
                    if obj.get("securityContext") is not None
                    else None
                ),
                "severity": obj.get("severity"),
                "target": (
                    [LogTarget.from_dict(_item) for _item in obj["target"]]
                    if obj.get("target") is not None
                    else None
                ),
                "transaction": (
                    LogTransaction.from_dict(obj["transaction"])
                    if obj.get("transaction") is not None
                    else None
                ),
                "uuid": obj.get("uuid"),
                "version": obj.get("version"),
            }
        )
        return _obj
