# coding: utf-8

"""
    Sentience API

    API Gateway for Sentience Platform - Web Content Extraction and Element Coordinate Mapping

    The version of the OpenAPI document: 1.0.0
    Contact: support@sentienceapi.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 datetime import datetime
from pydantic import BaseModel, ConfigDict, StrictBytes, StrictInt, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional, Union
from sentience_sdk.models.sentience_interactable_element import SentienceInteractableElement
from sentience_sdk.models.sentience_layout_viewport import SentienceLayoutViewport
from sentience_sdk.models.sentience_proxy_failure import SentienceProxyFailure
from typing import Optional, Set
from typing_extensions import Self

class SentienceVisualModeResponse(BaseModel):
    """
    Response for 'visual' mode
    """ # noqa: E501
    mode: StrictStr
    engine: Optional[StrictStr] = 'precision'
    source: Optional[StrictStr] = None
    status: StrictStr
    url: StrictStr
    layout_viewport: Optional[SentienceLayoutViewport] = None
    interactable_elements: List[SentienceInteractableElement]
    screenshot: Union[StrictBytes, StrictStr]
    screenshot_error: Optional[StrictStr] = None
    timestamp: Optional[datetime] = None
    total_elements_extracted: Optional[StrictInt] = None
    proxy_failure: Optional[SentienceProxyFailure] = None
    __properties: ClassVar[List[str]] = ["mode", "engine", "source", "status", "url", "layout_viewport", "interactable_elements", "screenshot", "screenshot_error", "timestamp", "total_elements_extracted", "proxy_failure"]

    @field_validator('mode')
    def mode_validate_enum(cls, value):
        """Validates the enum"""
        if value not in set(['visual']):
            raise ValueError("must be one of enum values ('visual')")
        return value

    @field_validator('engine')
    def engine_validate_enum(cls, value):
        """Validates the enum"""
        if value is None:
            return value

        if value not in set(['precision']):
            raise ValueError("must be one of enum values ('precision')")
        return value

    @field_validator('source')
    def source_validate_enum(cls, value):
        """Validates the enum"""
        if value is None:
            return value

        if value not in set(['precision_visual']):
            raise ValueError("must be one of enum values ('precision_visual')")
        return value

    @field_validator('status')
    def status_validate_enum(cls, value):
        """Validates the enum"""
        if value not in set(['success', 'error']):
            raise ValueError("must be one of enum values ('success', 'error')")
        return value

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    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) -> Optional[Self]:
        """Create an instance of SentienceVisualModeResponse 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.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of layout_viewport
        if self.layout_viewport:
            _dict['layout_viewport'] = self.layout_viewport.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in interactable_elements (list)
        _items = []
        if self.interactable_elements:
            for _item_interactable_elements in self.interactable_elements:
                if _item_interactable_elements:
                    _items.append(_item_interactable_elements.to_dict())
            _dict['interactable_elements'] = _items
        # override the default output from pydantic by calling `to_dict()` of proxy_failure
        if self.proxy_failure:
            _dict['proxy_failure'] = self.proxy_failure.to_dict()
        # set to None if screenshot_error (nullable) is None
        # and model_fields_set contains the field
        if self.screenshot_error is None and "screenshot_error" in self.model_fields_set:
            _dict['screenshot_error'] = None

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of SentienceVisualModeResponse from a dict"""
        if obj is None:
            return None

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

        _obj = cls.model_validate({
            "mode": obj.get("mode") if obj.get("mode") is not None else 'visual',
            "engine": obj.get("engine") if obj.get("engine") is not None else 'precision',
            "source": obj.get("source"),
            "status": obj.get("status"),
            "url": obj.get("url"),
            "layout_viewport": SentienceLayoutViewport.from_dict(obj["layout_viewport"]) if obj.get("layout_viewport") is not None else None,
            "interactable_elements": [SentienceInteractableElement.from_dict(_item) for _item in obj["interactable_elements"]] if obj.get("interactable_elements") is not None else None,
            "screenshot": obj.get("screenshot"),
            "screenshot_error": obj.get("screenshot_error"),
            "timestamp": obj.get("timestamp"),
            "total_elements_extracted": obj.get("total_elements_extracted"),
            "proxy_failure": SentienceProxyFailure.from_dict(obj["proxy_failure"]) if obj.get("proxy_failure") is not None else None
        })
        return _obj


