# coding: utf-8

"""
    Moodle Webservice API

    Auto-generated OpenAPI spec for Moodle's Webservice API.

    The version of the OpenAPI document: 5.0.3 (Build: 20251006)
    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 pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt
from typing import Any, ClassVar, Dict, List, Optional, Union
from poodle_async_full.models.mod_lesson_get_attempts_overview_response_data_students_inner import ModLessonGetAttemptsOverviewResponseDataStudentsInner
from typing import Optional, Set
from typing_extensions import Self

from poodle_async_full.configuration import settings

class ModLessonGetAttemptsOverviewResponseData(BaseModel):
    """
    Attempts overview data (empty for no attemps).
    """ # noqa: E501
    avescore: Optional[Union[StrictFloat, StrictInt]] = Field(description="Average score.")
    avetime: Optional[StrictInt] = Field(description="Average time (spent in taking the lesson).")
    highscore: Optional[Union[StrictFloat, StrictInt]] = Field(description="High score.")
    hightime: Optional[StrictInt] = Field(description="High time.")
    lessonscored: Optional[StrictBool] = Field(description="True if the lesson was scored.")
    lowscore: Optional[Union[StrictFloat, StrictInt]] = Field(description="Low score.")
    lowtime: Optional[StrictInt] = Field(description="Low time.")
    numofattempts: Optional[StrictInt] = Field(description="Number of attempts.")
    students: Optional[List[ModLessonGetAttemptsOverviewResponseDataStudentsInner]] = Field(default=None, description="Students data, including attempts.")
    __properties: ClassVar[List[str]] = ["avescore", "avetime", "highscore", "hightime", "lessonscored", "lowscore", "lowtime", "numofattempts", "students"]

    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 ModLessonGetAttemptsOverviewResponseData 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 each item in students (list)
        _items = []
        if self.students:
            for _item_students in self.students:
                if _item_students:
                    _items.append(_item_students.to_dict())
            _dict['students'] = _items
        # set to None if avescore (nullable) is None
        # and model_fields_set contains the field
        if self.avescore is None and "avescore" in self.model_fields_set:
            _dict['avescore'] = None

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

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

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

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

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

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

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

        return _dict

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

        relaxed = settings.relaxe_all_models or "ModLessonGetAttemptsOverviewResponseData" in settings.relaxed_models

        if not isinstance(obj, dict):
            return cls.model_construct(**obj) if relaxed else cls.model_validate(obj)

        data = {
            "avescore": obj.get("avescore"),
            "avetime": obj.get("avetime"),
            "highscore": obj.get("highscore"),
            "hightime": obj.get("hightime"),
            "lessonscored": obj.get("lessonscored"),
            "lowscore": obj.get("lowscore"),
            "lowtime": obj.get("lowtime"),
            "numofattempts": obj.get("numofattempts"),
            "students": [ModLessonGetAttemptsOverviewResponseDataStudentsInner.from_dict(_item) for _item in obj["students"]] if obj.get("students") is not None else None
        }
        _obj = cls.model_construct(**data) if relaxed else cls.model_validate(data)
        return _obj


