# 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, StrictBool, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from mixpeek.models.alert_match_result import AlertMatchResult
from typing import Optional, Set
from typing_extensions import Self

class AlertExecutionResult(BaseModel):
    """
    Result of an alert execution.  Captures the outcome of running an alert against one or more documents, including whether the alert was triggered and what matched.  Attributes:     alert_id: ID of the alert that was executed     collection_id: Collection the alert was executed against     execution_id: Unique identifier for this execution     triggered: Whether the alert was triggered (i.e., retriever returned results)     match_count: Number of matches found     matches: List of match results (if include_matches was True)     source_documents: Document IDs that triggered the alert check     executed_at: When the alert was executed     duration_ms: How long the execution took
    """ # noqa: E501
    alert_id: StrictStr = Field(description="ID of the alert that was executed")
    collection_id: StrictStr = Field(description="Collection the alert was executed against")
    execution_id: Optional[StrictStr] = Field(default=None, description="Unique identifier for this execution")
    triggered: StrictBool = Field(description="Whether the alert was triggered")
    match_count: Annotated[int, Field(strict=True, ge=0)] = Field(description="Number of matches found")
    matches: Optional[List[AlertMatchResult]] = Field(default=None, description="List of match results")
    source_documents: Optional[List[StrictStr]] = Field(default=None, description="Document IDs that triggered the alert check")
    executed_at: StrictStr = Field(description="ISO 8601 timestamp when the alert was executed")
    duration_ms: Annotated[int, Field(strict=True, ge=0)] = Field(description="How long the execution took in milliseconds")
    __properties: ClassVar[List[str]] = ["alert_id", "collection_id", "execution_id", "triggered", "match_count", "matches", "source_documents", "executed_at", "duration_ms"]

    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 AlertExecutionResult 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 matches (list)
        _items = []
        if self.matches:
            for _item_matches in self.matches:
                if _item_matches:
                    _items.append(_item_matches.to_dict())
            _dict['matches'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "alert_id": obj.get("alert_id"),
            "collection_id": obj.get("collection_id"),
            "execution_id": obj.get("execution_id"),
            "triggered": obj.get("triggered"),
            "match_count": obj.get("match_count"),
            "matches": [AlertMatchResult.from_dict(_item) for _item in obj["matches"]] if obj.get("matches") is not None else None,
            "source_documents": obj.get("source_documents"),
            "executed_at": obj.get("executed_at"),
            "duration_ms": obj.get("duration_ms")
        })
        return _obj


