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

class StageConfig(BaseModel):
    """
    Configuration for a single stage within a retriever.  Stages support dynamic configuration through template expressions using Jinja2 syntax.  IMPORTANT - Template Syntax:     - Use DOUBLE curly braces: {{INPUT.query}} (correct)     - Single curly braces will NOT work: {INPUT.query} (wrong - not substituted)     - Namespace names are CASE-INSENSITIVE: {{INPUT.query}}, {{inputs.query}}, {{input.query}}       all work identically  Template Namespaces (case-insensitive):     - INPUT / inputs / input: User-provided query parameters and inputs     - DOC / doc: Current document fields (for per-document logic)     - CONTEXT / context: Execution state (budget, timing, retriever metadata)     - STAGE / stage: Previous stage outputs (for cascading logic)  Examples:     Correct: {{INPUT.query_text}}, {{inputs.query}}, {{DOC.content_type}}     Correct: {{CONTEXT.budget_remaining}}, {{context.budget_remaining}}     Wrong:   {INPUT.query} - single braces won't be substituted
    """ # noqa: E501
    stage_name: Annotated[str, Field(min_length=1, strict=True)] = Field(description="Human-readable stage instance name (REQUIRED).")
    stage_type: Optional[StageType] = Field(default=None, description="Functional category of the stage. Optional for creation requests; auto-inferred from `stage_id` when omitted.")
    config: Dict[str, Any] = Field(description="Stage implementation parameters (REQUIRED). Must include `stage_id` key referencing a registered retriever stage. Supports template expressions using Jinja2 syntax resolved at execution time. Template namespaces support both uppercase and lowercase formats: {{INPUT.field}} or {{inputs.field}}, {{DOC.field}} or {{doc.field}}, {{CONTEXT.field}} or {{context.field}}, {{STAGE.field}} or {{stage.field}}. All formats work identically. Provide stage-specific configuration under `parameters`.")
    batch_size: Optional[StrictStr] = Field(default=None, description="Optional templated batch size expression evaluated per execution. Supports template variables: {{INPUT.page_size}}, {{inputs.page_size}}, {{CONTEXT.budget_remaining}}, etc. Both uppercase and lowercase namespace names are supported (e.g., INPUT/inputs, DOC/doc, CONTEXT/context, STAGE/stage). Defaults to stage-specific value when omitted.")
    description: Optional[StrictStr] = Field(default=None, description="User-facing description of the stage (OPTIONAL).")
    __properties: ClassVar[List[str]] = ["stage_name", "stage_type", "config", "batch_size", "description"]

    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 StageConfig 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,
        )
        return _dict

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

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

        _obj = cls.model_validate({
            "stage_name": obj.get("stage_name"),
            "stage_type": obj.get("stage_type"),
            "config": obj.get("config"),
            "batch_size": obj.get("batch_size"),
            "description": obj.get("description")
        })
        return _obj


