from __future__ import annotations

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

from attrs import define as _attrs_define
from attrs import field as _attrs_field
from dateutil.parser import isoparse

from ..models.knowledge_base_role import KnowledgeBaseRole
from ..types import UNSET, Unset

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


@_attrs_define
class KnowledgeBaseResponse:
    """KB enriched with stats for UI lists.

    Attributes:
        field_id (str):
        name (str):
        owner_user_id (str):
        tenant_id (str):
        current_user_role (KnowledgeBaseRole): Role within a knowledge base / workspace.
        description (None | str | Unset):
        created_at (datetime.datetime | Unset):
        updated_at (datetime.datetime | Unset):
        file_count (int | Unset):  Default: 0.
        member_count (int | Unset):  Default: 0.
    """

    field_id: str
    name: str
    owner_user_id: str
    tenant_id: str
    current_user_role: KnowledgeBaseRole
    description: None | str | Unset = UNSET
    created_at: datetime.datetime | Unset = UNSET
    updated_at: datetime.datetime | Unset = UNSET
    file_count: int | Unset = 0
    member_count: int | Unset = 0
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        field_id = self.field_id

        name = self.name

        owner_user_id = self.owner_user_id

        tenant_id = self.tenant_id

        current_user_role = self.current_user_role.value

        description: None | str | Unset
        if isinstance(self.description, Unset):
            description = UNSET
        else:
            description = self.description

        created_at: str | Unset = UNSET
        if not isinstance(self.created_at, Unset):
            created_at = self.created_at.isoformat()

        updated_at: str | Unset = UNSET
        if not isinstance(self.updated_at, Unset):
            updated_at = self.updated_at.isoformat()

        file_count = self.file_count

        member_count = self.member_count

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "_id": field_id,
                "name": name,
                "owner_user_id": owner_user_id,
                "tenant_id": tenant_id,
                "current_user_role": current_user_role,
            }
        )
        if description is not UNSET:
            field_dict["description"] = description
        if created_at is not UNSET:
            field_dict["created_at"] = created_at
        if updated_at is not UNSET:
            field_dict["updated_at"] = updated_at
        if file_count is not UNSET:
            field_dict["file_count"] = file_count
        if member_count is not UNSET:
            field_dict["member_count"] = member_count

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        d = dict(src_dict)
        field_id = d.pop("_id")

        name = d.pop("name")

        owner_user_id = d.pop("owner_user_id")

        tenant_id = d.pop("tenant_id")

        current_user_role = KnowledgeBaseRole(d.pop("current_user_role"))

        def _parse_description(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        description = _parse_description(d.pop("description", UNSET))

        _created_at = d.pop("created_at", UNSET)
        created_at: datetime.datetime | Unset
        if isinstance(_created_at, Unset):
            created_at = UNSET
        else:
            created_at = isoparse(_created_at)

        _updated_at = d.pop("updated_at", UNSET)
        updated_at: datetime.datetime | Unset
        if isinstance(_updated_at, Unset):
            updated_at = UNSET
        else:
            updated_at = isoparse(_updated_at)

        file_count = d.pop("file_count", UNSET)

        member_count = d.pop("member_count", UNSET)

        knowledge_base_response = cls(
            field_id=field_id,
            name=name,
            owner_user_id=owner_user_id,
            tenant_id=tenant_id,
            current_user_role=current_user_role,
            description=description,
            created_at=created_at,
            updated_at=updated_at,
            file_count=file_count,
            member_count=member_count,
        )

        knowledge_base_response.additional_properties = d
        return knowledge_base_response

    @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
