# coding: utf-8

"""
    Instana REST API documentation

    Documentation for INSTANA REST API

    The version of the OpenAPI document: 1.309.1268
    Contact: support@instana.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 instana_client.models.endpoint import Endpoint
from instana_client.models.service import Service
from typing import Optional, Set
from typing_extensions import Self

class Trace(BaseModel):
    """
    1. `service`: The service from where trace started. 2. `endpoint`: Endpoint of the service. 
    """ # noqa: E501
    duration: Optional[Annotated[int, Field(strict=True, ge=0)]] = Field(default=None, description="Total time taken for a trace to finish.")
    endpoint: Optional[Endpoint] = None
    erroneous: Optional[StrictBool] = Field(default=None, description="Flag which tells whether the trace is erroneous or not")
    id: StrictStr = Field(description="The trace ID. All spans of the same trace must have the same trace ID. For example, `e93282c0d5018320`.")
    label: StrictStr = Field(description="Name of the trace.")
    service: Optional[Service] = None
    start_time: Optional[Annotated[int, Field(strict=True, ge=1)]] = Field(default=None, description="The start time of the trace.", alias="startTime")
    __properties: ClassVar[List[str]] = ["duration", "endpoint", "erroneous", "id", "label", "service", "startTime"]

    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 Trace 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 endpoint
        if self.endpoint:
            _dict['endpoint'] = self.endpoint.to_dict()
        # override the default output from pydantic by calling `to_dict()` of service
        if self.service:
            _dict['service'] = self.service.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "duration": obj.get("duration"),
            "endpoint": Endpoint.from_dict(obj["endpoint"]) if obj.get("endpoint") is not None else None,
            "erroneous": obj.get("erroneous"),
            "id": obj.get("id"),
            "label": obj.get("label"),
            "service": Service.from_dict(obj["service"]) if obj.get("service") is not None else None,
            "startTime": obj.get("startTime")
        })
        return _obj


