from __future__ import annotations

from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeVar, cast

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.usage_input_tokens_details import UsageInputTokensDetails
    from ..models.usage_output_tokens_details import UsageOutputTokensDetails


T = TypeVar("T", bound="Usage")


@_attrs_define
class Usage:
    """For `gpt-image-1` only, the token usage information for the image generation.

    Attributes:
        input_tokens (int):
        input_tokens_details (UsageInputTokensDetails): The input tokens detailed information for the image generation.
        output_tokens (int):
        total_tokens (int):
        output_tokens_details (None | Unset | UsageOutputTokensDetails):
    """

    input_tokens: int
    input_tokens_details: UsageInputTokensDetails
    output_tokens: int
    total_tokens: int
    output_tokens_details: None | Unset | UsageOutputTokensDetails = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.usage_output_tokens_details import UsageOutputTokensDetails

        input_tokens = self.input_tokens

        input_tokens_details = self.input_tokens_details.to_dict()

        output_tokens = self.output_tokens

        total_tokens = self.total_tokens

        output_tokens_details: dict[str, Any] | None | Unset
        if isinstance(self.output_tokens_details, Unset):
            output_tokens_details = UNSET
        elif isinstance(self.output_tokens_details, UsageOutputTokensDetails):
            output_tokens_details = self.output_tokens_details.to_dict()
        else:
            output_tokens_details = self.output_tokens_details

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "input_tokens": input_tokens,
                "input_tokens_details": input_tokens_details,
                "output_tokens": output_tokens,
                "total_tokens": total_tokens,
            }
        )
        if output_tokens_details is not UNSET:
            field_dict["output_tokens_details"] = output_tokens_details

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.usage_input_tokens_details import UsageInputTokensDetails
        from ..models.usage_output_tokens_details import UsageOutputTokensDetails

        d = dict(src_dict)
        input_tokens = d.pop("input_tokens")

        input_tokens_details = UsageInputTokensDetails.from_dict(d.pop("input_tokens_details"))

        output_tokens = d.pop("output_tokens")

        total_tokens = d.pop("total_tokens")

        def _parse_output_tokens_details(data: object) -> None | Unset | UsageOutputTokensDetails:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                output_tokens_details_type_0 = UsageOutputTokensDetails.from_dict(data)

                return output_tokens_details_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(None | Unset | UsageOutputTokensDetails, data)

        output_tokens_details = _parse_output_tokens_details(d.pop("output_tokens_details", UNSET))

        usage = cls(
            input_tokens=input_tokens,
            input_tokens_details=input_tokens_details,
            output_tokens=output_tokens,
            total_tokens=total_tokens,
            output_tokens_details=output_tokens_details,
        )

        usage.additional_properties = d
        return usage

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
