# coding: utf-8

"""
Perigon API

The Perigon API provides access to comprehensive news and web content data. To use the API, simply sign up for a Perigon Business Solutions account to obtain your API key. Your available features may vary based on your plan. See the Authentication section for details on how to use your API key.

The version of the OpenAPI document: 1.0.0
Contact: data@perigon.io
Generated by OpenAPI Generator (https://openapi-generator.tech)

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


from __future__ import annotations

import json
import pprint
import re  # noqa: F401
from typing import Any, ClassVar, Dict, List, Optional, Set

from pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing_extensions import Self

from perigon.models.coordinate import Coordinate


class Place(BaseModel):
    """
    Place
    """  # noqa: E501

    osm_id: Optional[StrictStr] = Field(default=None, alias="osmId")
    road: Optional[StrictStr] = None
    quarter: Optional[StrictStr] = None
    suburb: Optional[StrictStr] = None
    city: Optional[StrictStr] = None
    town: Optional[StrictStr] = None
    county: Optional[StrictStr] = None
    state_district: Optional[StrictStr] = Field(default=None, alias="stateDistrict")
    state: Optional[StrictStr] = None
    postcode: Optional[StrictStr] = None
    country: Optional[StrictStr] = None
    country_code: Optional[StrictStr] = Field(default=None, alias="countryCode")
    amenity: Optional[StrictStr] = None
    neighbourhood: Optional[StrictStr] = None
    coordinates: Optional[Coordinate] = None
    __properties: ClassVar[List[str]] = [
        "osmId",
        "road",
        "quarter",
        "suburb",
        "city",
        "town",
        "county",
        "stateDistrict",
        "state",
        "postcode",
        "country",
        "countryCode",
        "amenity",
        "neighbourhood",
        "coordinates",
    ]

    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 Place 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 coordinates
        if self.coordinates:
            _dict["coordinates"] = self.coordinates.to_dict()
        # set to None if osm_id (nullable) is None
        # and model_fields_set contains the field
        if self.osm_id is None and "osm_id" in self.model_fields_set:
            _dict["osmId"] = None

        # set to None if road (nullable) is None
        # and model_fields_set contains the field
        if self.road is None and "road" in self.model_fields_set:
            _dict["road"] = None

        # set to None if quarter (nullable) is None
        # and model_fields_set contains the field
        if self.quarter is None and "quarter" in self.model_fields_set:
            _dict["quarter"] = None

        # set to None if suburb (nullable) is None
        # and model_fields_set contains the field
        if self.suburb is None and "suburb" in self.model_fields_set:
            _dict["suburb"] = None

        # set to None if city (nullable) is None
        # and model_fields_set contains the field
        if self.city is None and "city" in self.model_fields_set:
            _dict["city"] = None

        # set to None if town (nullable) is None
        # and model_fields_set contains the field
        if self.town is None and "town" in self.model_fields_set:
            _dict["town"] = None

        # set to None if county (nullable) is None
        # and model_fields_set contains the field
        if self.county is None and "county" in self.model_fields_set:
            _dict["county"] = None

        # set to None if state_district (nullable) is None
        # and model_fields_set contains the field
        if self.state_district is None and "state_district" in self.model_fields_set:
            _dict["stateDistrict"] = None

        # set to None if state (nullable) is None
        # and model_fields_set contains the field
        if self.state is None and "state" in self.model_fields_set:
            _dict["state"] = None

        # set to None if postcode (nullable) is None
        # and model_fields_set contains the field
        if self.postcode is None and "postcode" in self.model_fields_set:
            _dict["postcode"] = None

        # set to None if country (nullable) is None
        # and model_fields_set contains the field
        if self.country is None and "country" in self.model_fields_set:
            _dict["country"] = None

        # set to None if country_code (nullable) is None
        # and model_fields_set contains the field
        if self.country_code is None and "country_code" in self.model_fields_set:
            _dict["countryCode"] = None

        # set to None if amenity (nullable) is None
        # and model_fields_set contains the field
        if self.amenity is None and "amenity" in self.model_fields_set:
            _dict["amenity"] = None

        # set to None if neighbourhood (nullable) is None
        # and model_fields_set contains the field
        if self.neighbourhood is None and "neighbourhood" in self.model_fields_set:
            _dict["neighbourhood"] = None

        # set to None if coordinates (nullable) is None
        # and model_fields_set contains the field
        if self.coordinates is None and "coordinates" in self.model_fields_set:
            _dict["coordinates"] = None

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "osmId": obj.get("osmId"),
                "road": obj.get("road"),
                "quarter": obj.get("quarter"),
                "suburb": obj.get("suburb"),
                "city": obj.get("city"),
                "town": obj.get("town"),
                "county": obj.get("county"),
                "stateDistrict": obj.get("stateDistrict"),
                "state": obj.get("state"),
                "postcode": obj.get("postcode"),
                "country": obj.get("country"),
                "countryCode": obj.get("countryCode"),
                "amenity": obj.get("amenity"),
                "neighbourhood": obj.get("neighbourhood"),
                "coordinates": (
                    Coordinate.from_dict(obj["coordinates"])
                    if obj.get("coordinates") is not None
                    else None
                ),
            }
        )
        return _obj
