# coding: utf-8

"""
    AI Hub API

    The AI Hub REST API. See https://docs.instabase.com/api-sdk/ for more details.

    The version of the OpenAPI document: 0.1
    Contact: support@instabase.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json


from typing import Any, ClassVar, Dict, List, Optional
from pydantic import BaseModel, StrictStr
from pydantic import Field
from aihub.models.document_field_confidence import DocumentFieldConfidence
from aihub.models.document_field_validations import DocumentFieldValidations
from aihub.models.field_source_coordinates import FieldSourceCoordinates
from aihub.models.modification_object import ModificationObject
try:
    from typing import Self
except ImportError:
    from typing_extensions import Self

class DocumentField(BaseModel):
    """
    DocumentField
    """ # noqa: E501
    field_name: Optional[StrictStr] = Field(default=None, description="The name of the field.")
    value: Optional[StrictStr] = Field(default=None, description="The extracted value of the field.")
    type: Optional[StrictStr] = Field(default=None, description="The type of the field.")
    source_coordinates: Optional[List[FieldSourceCoordinates]] = None
    edit_history: Optional[List[ModificationObject]] = Field(default=None, description="An array containing the history of edits to the field.")
    confidence: Optional[DocumentFieldConfidence] = None
    validations: Optional[DocumentFieldValidations] = None
    __properties: ClassVar[List[str]] = ["field_name", "value", "type", "source_coordinates", "edit_history", "confidence", "validations"]

    model_config = {
        "populate_by_name": True,
        "validate_assignment": True
    }


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Self:
        """Create an instance of DocumentField from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        _dict = self.model_dump(
            by_alias=True,
            exclude={
            },
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in source_coordinates (list)
        _items = []
        if self.source_coordinates:
            for _item in self.source_coordinates:
                if _item:
                    _items.append(_item.to_dict())
            _dict['source_coordinates'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in edit_history (list)
        _items = []
        if self.edit_history:
            for _item in self.edit_history:
                if _item:
                    _items.append(_item.to_dict())
            _dict['edit_history'] = _items
        # override the default output from pydantic by calling `to_dict()` of confidence
        if self.confidence:
            _dict['confidence'] = self.confidence.to_dict()
        # override the default output from pydantic by calling `to_dict()` of validations
        if self.validations:
            _dict['validations'] = self.validations.to_dict()
        return _dict

    @classmethod
    def from_dict(cls, obj: Dict) -> Self:
        """Create an instance of DocumentField from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "field_name": obj.get("field_name"),
            "value": obj.get("value"),
            "type": obj.get("type"),
            "source_coordinates": [FieldSourceCoordinates.from_dict(_item) for _item in obj.get("source_coordinates")] if obj.get("source_coordinates") is not None else None,
            "edit_history": [ModificationObject.from_dict(_item) for _item in obj.get("edit_history")] if obj.get("edit_history") is not None else None,
            "confidence": DocumentFieldConfidence.from_dict(obj.get("confidence")) if obj.get("confidence") is not None else None,
            "validations": DocumentFieldValidations.from_dict(obj.get("validations")) if obj.get("validations") is not None else None
        })
        return _obj


