# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from typing import cast
from typing import Any, Dict, List, Literal, Optional
from workos._types import _raise_deserialize_error
from workos._types import _format_datetime, _parse_datetime

from workos.common.models.organization_domain import OrganizationDomain


@dataclass(slots=True)
class Organization:
    """Organization model."""

    object: Literal["organization"]
    """Distinguishes the Organization object."""
    id: str
    """Unique identifier of the Organization."""
    name: str
    """A descriptive name for the Organization. This field does not need to be unique."""
    domains: List["OrganizationDomain"]
    """List of Organization Domains."""
    metadata: Dict[str, str]
    """Object containing [metadata](https://workos.com/docs/authkit/metadata) key/value pairs associated with the Organization."""
    external_id: Optional[str]
    """The external ID of the Organization."""
    created_at: datetime
    """An ISO 8601 timestamp."""
    updated_at: datetime
    """An ISO 8601 timestamp."""
    stripe_customer_id: Optional[str] = None
    """The Stripe customer ID of the Organization."""
    allow_profiles_outside_organization: Optional[bool] = None
    """Whether the Organization allows profiles outside of its managed domains.

    .. deprecated:: This field is deprecated."""

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "Organization":
        """Deserialize from a dictionary."""
        try:
            return cls(
                object=data.get("object", "organization"),
                id=data["id"],
                name=data["name"],
                domains=[
                    OrganizationDomain.from_dict(cast(Dict[str, Any], item))
                    for item in cast(list[Any], data["domains"])
                ],
                metadata=data["metadata"],
                external_id=data["external_id"],
                created_at=_parse_datetime(data["created_at"]),
                updated_at=_parse_datetime(data["updated_at"]),
                stripe_customer_id=data.get("stripe_customer_id"),
                allow_profiles_outside_organization=data.get(
                    "allow_profiles_outside_organization"
                ),
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("Organization", e)

    def to_dict(self) -> Dict[str, Any]:
        """Serialize to a dictionary."""
        result: Dict[str, Any] = {}
        result["object"] = self.object
        result["id"] = self.id
        result["name"] = self.name
        result["domains"] = [item.to_dict() for item in self.domains]
        result["metadata"] = self.metadata
        if self.external_id is not None:
            result["external_id"] = self.external_id
        else:
            result["external_id"] = None
        result["created_at"] = _format_datetime(self.created_at)
        result["updated_at"] = _format_datetime(self.updated_at)
        if self.stripe_customer_id is not None:
            result["stripe_customer_id"] = self.stripe_customer_id
        if self.allow_profiles_outside_organization is not None:
            result["allow_profiles_outside_organization"] = (
                self.allow_profiles_outside_organization
            )
        return result
