# coding: utf-8

"""
    Sonarr

    Sonarr API docs - The v3 API docs apply to both v3 and v4 versions of Sonarr. Some functionality may only be available in v4 of the Sonarr application.

    The version of the OpenAPI document: v4.0.12.2823
    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, Field, StrictBool, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from sonarr.models.episode_file_resource import EpisodeFileResource
from sonarr.models.media_cover import MediaCover
from sonarr.models.series_resource import SeriesResource
from typing import Optional, Set
from typing_extensions import Self

class EpisodeResource(BaseModel):
    """
    EpisodeResource
    """ # noqa: E501
    id: Optional[StrictInt] = None
    series_id: Optional[StrictInt] = Field(default=None, alias="seriesId")
    tvdb_id: Optional[StrictInt] = Field(default=None, alias="tvdbId")
    episode_file_id: Optional[StrictInt] = Field(default=None, alias="episodeFileId")
    season_number: Optional[StrictInt] = Field(default=None, alias="seasonNumber")
    episode_number: Optional[StrictInt] = Field(default=None, alias="episodeNumber")
    title: Optional[StrictStr] = None
    air_date: Optional[StrictStr] = Field(default=None, alias="airDate")
    air_date_utc: Optional[datetime] = Field(default=None, alias="airDateUtc")
    last_search_time: Optional[datetime] = Field(default=None, alias="lastSearchTime")
    runtime: Optional[StrictInt] = None
    finale_type: Optional[StrictStr] = Field(default=None, alias="finaleType")
    overview: Optional[StrictStr] = None
    episode_file: Optional[EpisodeFileResource] = Field(default=None, alias="episodeFile")
    has_file: Optional[StrictBool] = Field(default=None, alias="hasFile")
    monitored: Optional[StrictBool] = None
    absolute_episode_number: Optional[StrictInt] = Field(default=None, alias="absoluteEpisodeNumber")
    scene_absolute_episode_number: Optional[StrictInt] = Field(default=None, alias="sceneAbsoluteEpisodeNumber")
    scene_episode_number: Optional[StrictInt] = Field(default=None, alias="sceneEpisodeNumber")
    scene_season_number: Optional[StrictInt] = Field(default=None, alias="sceneSeasonNumber")
    unverified_scene_numbering: Optional[StrictBool] = Field(default=None, alias="unverifiedSceneNumbering")
    end_time: Optional[datetime] = Field(default=None, alias="endTime")
    grab_date: Optional[datetime] = Field(default=None, alias="grabDate")
    series: Optional[SeriesResource] = None
    images: Optional[List[MediaCover]] = None
    __properties: ClassVar[List[str]] = ["id", "seriesId", "tvdbId", "episodeFileId", "seasonNumber", "episodeNumber", "title", "airDate", "airDateUtc", "lastSearchTime", "runtime", "finaleType", "overview", "episodeFile", "hasFile", "monitored", "absoluteEpisodeNumber", "sceneAbsoluteEpisodeNumber", "sceneEpisodeNumber", "sceneSeasonNumber", "unverifiedSceneNumbering", "endTime", "grabDate", "series", "images"]

    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 EpisodeResource 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 episode_file
        if self.episode_file:
            _dict['episodeFile'] = self.episode_file.to_dict()
        # override the default output from pydantic by calling `to_dict()` of series
        if self.series:
            _dict['series'] = self.series.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in images (list)
        _items = []
        if self.images:
            for _item_images in self.images:
                if _item_images:
                    _items.append(_item_images.to_dict())
            _dict['images'] = _items
        # set to None if title (nullable) is None
        # and model_fields_set contains the field
        if self.title is None and "title" in self.model_fields_set:
            _dict['title'] = None

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

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

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

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

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

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

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

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "id": obj.get("id"),
            "seriesId": obj.get("seriesId"),
            "tvdbId": obj.get("tvdbId"),
            "episodeFileId": obj.get("episodeFileId"),
            "seasonNumber": obj.get("seasonNumber"),
            "episodeNumber": obj.get("episodeNumber"),
            "title": obj.get("title"),
            "airDate": obj.get("airDate"),
            "airDateUtc": obj.get("airDateUtc"),
            "lastSearchTime": obj.get("lastSearchTime"),
            "runtime": obj.get("runtime"),
            "finaleType": obj.get("finaleType"),
            "overview": obj.get("overview"),
            "episodeFile": EpisodeFileResource.from_dict(obj["episodeFile"]) if obj.get("episodeFile") is not None else None,
            "hasFile": obj.get("hasFile"),
            "monitored": obj.get("monitored"),
            "absoluteEpisodeNumber": obj.get("absoluteEpisodeNumber"),
            "sceneAbsoluteEpisodeNumber": obj.get("sceneAbsoluteEpisodeNumber"),
            "sceneEpisodeNumber": obj.get("sceneEpisodeNumber"),
            "sceneSeasonNumber": obj.get("sceneSeasonNumber"),
            "unverifiedSceneNumbering": obj.get("unverifiedSceneNumbering"),
            "endTime": obj.get("endTime"),
            "grabDate": obj.get("grabDate"),
            "series": SeriesResource.from_dict(obj["series"]) if obj.get("series") is not None else None,
            "images": [MediaCover.from_dict(_item) for _item in obj["images"]] if obj.get("images") is not None else None
        })
        return _obj


