# coding: utf-8

"""
    Mixpeek API

    This is the Mixpeek API, providing access to various endpoints for data processing and retrieval.

    The version of the OpenAPI document: 1.3.0
    Contact: info@mixpeek.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 pydantic import BaseModel, ConfigDict, Field, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from mixpeek.models.alignment_metrics import AlignmentMetrics
from mixpeek.models.latency_metrics import LatencyMetrics
from typing import Optional, Set
from typing_extensions import Self

class BenchmarkResult(BaseModel):
    """
    Results for a single pipeline in a benchmark run.
    """ # noqa: E501
    retriever_id: StrictStr = Field(description="ID of the retriever/pipeline tested.")
    retriever_name: StrictStr = Field(description="Human-readable name of the retriever.")
    pipeline_hash: StrictStr = Field(description="Hash of the pipeline configuration.")
    metrics: AlignmentMetrics = Field(description="Alignment metrics for this pipeline.")
    taxonomy_deltas: Optional[Dict[str, AlignmentMetrics]] = Field(default=None, description="Metrics broken down by taxonomy node (for understanding category-level performance).")
    latency: LatencyMetrics = Field(description="Performance timing statistics.")
    failed_sessions: Annotated[int, Field(strict=True, ge=0)] = Field(description="Number of sessions that failed during replay.")
    error_summary: Optional[Dict[str, StrictInt]] = Field(default=None, description="Count of errors by type (error_type -> count).")
    __properties: ClassVar[List[str]] = ["retriever_id", "retriever_name", "pipeline_hash", "metrics", "taxonomy_deltas", "latency", "failed_sessions", "error_summary"]

    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 BenchmarkResult 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 metrics
        if self.metrics:
            _dict['metrics'] = self.metrics.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each value in taxonomy_deltas (dict)
        _field_dict = {}
        if self.taxonomy_deltas:
            for _key_taxonomy_deltas in self.taxonomy_deltas:
                if self.taxonomy_deltas[_key_taxonomy_deltas]:
                    _field_dict[_key_taxonomy_deltas] = self.taxonomy_deltas[_key_taxonomy_deltas].to_dict()
            _dict['taxonomy_deltas'] = _field_dict
        # override the default output from pydantic by calling `to_dict()` of latency
        if self.latency:
            _dict['latency'] = self.latency.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "retriever_id": obj.get("retriever_id"),
            "retriever_name": obj.get("retriever_name"),
            "pipeline_hash": obj.get("pipeline_hash"),
            "metrics": AlignmentMetrics.from_dict(obj["metrics"]) if obj.get("metrics") is not None else None,
            "taxonomy_deltas": dict(
                (_k, AlignmentMetrics.from_dict(_v))
                for _k, _v in obj["taxonomy_deltas"].items()
            )
            if obj.get("taxonomy_deltas") is not None
            else None,
            "latency": LatencyMetrics.from_dict(obj["latency"]) if obj.get("latency") is not None else None,
            "failed_sessions": obj.get("failed_sessions"),
            "error_summary": obj.get("error_summary")
        })
        return _obj


