# ********************************************************************************
# *         WARNING: This file is automatically generated by unasync.py.         *
# *                             DO NOT MANUALLY EDIT                             *
# *              Source File: patent_client/_async/epo/ops/util.py               *
# ********************************************************************************

from typing import TYPE_CHECKING, List, Optional

from async_property.base import AsyncPropertyDescriptor
from pydantic import ConfigDict, computed_field, model_validator
from yankee.xml.schema import Schema as XmlSchema

from patent_client.util.pydantic_util import BaseModel

if TYPE_CHECKING:
    from .family.model import FamilyMember
    from .legal.model import LegalEvent
    from .published.model import Claims, Images, InpadocBiblio


class Schema(XmlSchema):
    class Meta:
        namespaces = {
            "ops": "http://ops.epo.org",
            "epo": "http://www.epo.org/exchange",
            "ft": "http://www.epo.org/fulltext",
        }


class EpoBaseModel(BaseModel):
    model_config = ConfigDict(ignored_types=(AsyncPropertyDescriptor,))
    __schema__: Optional[XmlSchema] = None

    @model_validator(mode="before")
    @classmethod
    def xml_convert(cls, values):
        if isinstance(values, (str, bytes)):
            return cls.__schema__.load(values).to_dict()
        return values


class InpadocModel(EpoBaseModel):
    @computed_field
    @property
    def docdb_number(self) -> str:
        num = self.publication_reference_docdb
        if num.kind:
            return f"{num.country}{num.number}.{num.kind}"
        return f"{num.country}{num.number}"

    @property
    def biblio(self) -> "InpadocBiblio":
        return self._get_model(
            ".published.model.InpadocBiblio", base_class=InpadocModel
        ).objects.get(self.docdb_number)

    @property
    def images(self) -> "Images":
        return self._get_model(".published.model.Images", base_class=InpadocModel).objects.get(
            self.docdb_number
        )

    @property
    def description(self) -> Optional[str]:
        return (
            self._get_model(".published.model.Description", base_class=InpadocModel).objects.get(
                self.docdb_number
            )
        ).description

    @property
    def claims(self) -> "Claims":
        return self._get_model(".published.model.Claims", base_class=InpadocModel).objects.get(
            self.docdb_number
        )

    @property
    def legal(self) -> List["LegalEvent"]:
        return (
            self._get_model(".legal.model.Legal", base_class=InpadocModel).objects.get(
                self.docdb_number
            )
        ).events

    @property
    def family(
        self,
    ) -> List["FamilyMember"]:
        return (
            self._get_model(".family.model.Family", base_class=InpadocModel).objects.get(
                self.docdb_number
            )
        ).family_members

    def download(self, path: str = "."):
        images = self.images
        return images.full_document.download(path)
