# 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 typing import Any, ClassVar, Dict, List
from typing import Optional, Set

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

from okta.models.managed_connection_patchable_scope_condition import (
    ManagedConnectionPatchableScopeCondition,
)


class ManagedConnectionPatchable(BaseModel):
    """
    Update an existing managed connection. All fields are optional for partial updates.  **Field Applicability by Connection
    Type:** - `resourceIndicator`: Valid for IDENTITY_ASSERTION_APP_INSTANCE, IDENTITY_ASSERTION_CUSTOM_AS,
    STS_SERVICE_ACCOUNT, and STS_VAULT_SECRET. Set to `null` to reset to the default value (the resource's ORN). -
    `scopeCondition` and `scopes`: Only valid for IDENTITY_ASSERTION_APP_INSTANCE and IDENTITY_ASSERTION_CUSTOM_AS
    connections. The server returns a validation error if these fields are sent for STS connection types.  **Validation:** -
    If `scopeCondition` is provided, `scopes` must also be provided, and vice versa. - For STS_VAULT_SECRET and
    STS_SERVICE_ACCOUNT connection types, only `resourceIndicator` can be updated.
    """  # noqa: E501

    resource_indicator: Optional[StrictStr] = Field(
        default=None,
        description="Resource indicator used when requesting tokens. Set to `null` to reset to the default value based on "
        "the connection type (app instance ORN, authorization server ORN, secret ORN, or service account ORN).",
        alias="resourceIndicator",
    )
    scope_condition: Optional[ManagedConnectionPatchableScopeCondition] = Field(
        default=None, alias="scopeCondition"
    )
    scopes: Optional[Annotated[List[StrictStr], Field(min_length=1)]] = Field(
        default=None,
        description="Array of scopes for the connection. For `ALL_SCOPES`, this array must contain a single value of `*`. "
        "For `INCLUDE_ONLY`, only these scopes are allowed. For `EXCLUDE`, all scopes except these are allowed.  "
        "**Restrictions:** - Only valid for IDENTITY_ASSERTION_APP_INSTANCE and IDENTITY_ASSERTION_CUSTOM_AS "
        "connection types - Must be provided together with `scopeCondition` - Returns a 400 error if sent for "
        "STS_VAULT_SECRET or STS_SERVICE_ACCOUNT connection types",
    )
    __properties: ClassVar[List[str]] = [
        "resourceIndicator",
        "scopeCondition",
        "scopes",
    ]

    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 ManagedConnectionPatchable 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,
        )
        # set to None if resource_indicator (nullable) is None
        # and model_fields_set contains the field
        if (
            self.resource_indicator is None
            and "resource_indicator" in self.model_fields_set
        ):
            _dict["resourceIndicator"] = None

        # set to None if scope_condition (nullable) is None
        # and model_fields_set contains the field
        if self.scope_condition is None and "scope_condition" in self.model_fields_set:
            _dict["scopeCondition"] = None

        # set to None if scopes (nullable) is None
        # and model_fields_set contains the field
        if self.scopes is None and "scopes" in self.model_fields_set:
            _dict["scopes"] = None

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "resourceIndicator": obj.get("resourceIndicator"),
                "scopeCondition": obj.get("scopeCondition"),
                "scopes": obj.get("scopes"),
            }
        )
        return _obj
