"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .byokproviderslug import BYOKProviderSlug
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from openrouter.utils import validate_open_enum
from pydantic import model_serializer
from pydantic.functional_validators import PlainValidator
from typing import List
from typing_extensions import Annotated, NotRequired, TypedDict


class BYOKKeyTypedDict(TypedDict):
    allowed_api_key_hashes: Nullable[List[str]]
    r"""Optional allowlist of OpenRouter API key hashes (`api_keys.hash`) that may use this credential. `null` means no restriction."""
    allowed_models: Nullable[List[str]]
    r"""Optional allowlist of model slugs this credential may be used for. `null` means no restriction."""
    allowed_user_ids: Nullable[List[str]]
    r"""Optional allowlist of user IDs that may use this credential. `null` means no restriction."""
    created_at: str
    r"""ISO timestamp of when the credential was created."""
    disabled: bool
    r"""Whether this credential is currently disabled."""
    id: str
    r"""Stable public identifier for this BYOK credential."""
    is_fallback: bool
    r"""Whether this credential is treated as a fallback — used only after non-fallback keys for the same provider have been tried."""
    label: str
    r"""Short masked snippet of the key (e.g. the first/last few characters) used to identify it in the UI."""
    provider: BYOKProviderSlug
    r"""The upstream provider this credential authenticates against, as a lowercase slug (e.g. `openai`, `anthropic`, `amazon-bedrock`)."""
    sort_order: int
    r"""Position within the provider — credentials are tried in ascending sort order."""
    workspace_id: str
    r"""ID of the workspace this credential belongs to."""
    name: NotRequired[Nullable[str]]
    r"""Optional human-readable name for the credential."""


class BYOKKey(BaseModel):
    allowed_api_key_hashes: Nullable[List[str]]
    r"""Optional allowlist of OpenRouter API key hashes (`api_keys.hash`) that may use this credential. `null` means no restriction."""

    allowed_models: Nullable[List[str]]
    r"""Optional allowlist of model slugs this credential may be used for. `null` means no restriction."""

    allowed_user_ids: Nullable[List[str]]
    r"""Optional allowlist of user IDs that may use this credential. `null` means no restriction."""

    created_at: str
    r"""ISO timestamp of when the credential was created."""

    disabled: bool
    r"""Whether this credential is currently disabled."""

    id: str
    r"""Stable public identifier for this BYOK credential."""

    is_fallback: bool
    r"""Whether this credential is treated as a fallback — used only after non-fallback keys for the same provider have been tried."""

    label: str
    r"""Short masked snippet of the key (e.g. the first/last few characters) used to identify it in the UI."""

    provider: Annotated[BYOKProviderSlug, PlainValidator(validate_open_enum(False))]
    r"""The upstream provider this credential authenticates against, as a lowercase slug (e.g. `openai`, `anthropic`, `amazon-bedrock`)."""

    sort_order: int
    r"""Position within the provider — credentials are tried in ascending sort order."""

    workspace_id: str
    r"""ID of the workspace this credential belongs to."""

    name: OptionalNullable[str] = UNSET
    r"""Optional human-readable name for the credential."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["name"]
        nullable_fields = [
            "allowed_api_key_hashes",
            "allowed_models",
            "allowed_user_ids",
            "name",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
