"""Pydantic models for workflow metadata.json files.

The metadata.json file is generated alongside workflow code to ensure stable,
UI-aligned identifiers without relying on Python display classes. It contains
mappings between code paths and UI identifiers for nodes, triggers, edges, and
dataset rows.
"""

from typing import Dict, Optional

from vellum.client.core.pydantic_utilities import UniversalBaseModel
from vellum.client.types.code_resource_definition import CodeResourceDefinition


class RunnerConfig(UniversalBaseModel):
    """Configuration for the workflow runner environment.

    This specifies the container image and version information used to execute
    the workflow.
    """

    container_image_name: Optional[str] = None
    """The name of the container image to use. If not provided, defaults to the
    standard Python workflow runtime image."""

    container_image_tag: Optional[str] = None
    """The tag of the container image to use. If not provided, defaults to the
    latest tag."""

    codegen_version: Optional[str] = None
    """The version of codegen used to generate the workflow artifact.

    .. deprecated::
        This field is deprecated. Use `sdk_version` instead.
    """

    sdk_version: Optional[str] = None
    """The SDK version the workflow is intended to run on."""

    is_deployment_inlining_enabled: bool = False
    """Whether deployment inlining is enabled for this workflow."""

    server_version: Optional[str] = None
    """The server version the workflow is intended to run on. This gets set when
    deploying a workflow with a hotswappable custom image."""


class WorkflowMetadata(UniversalBaseModel):
    """Metadata persisted alongside generated workflow code.

    This metadata ensures stable, UI-aligned identifiers without relying on
    Python display classes. It is generated by the codegen system and consumed
    during workflow serialization to maintain consistent IDs across round-trips.
    """

    runner_config: Optional[RunnerConfig] = None
    """Configuration for the workflow runner environment."""

    label: Optional[str] = None
    """Human-readable label for the workflow."""

    codegen_version: Optional[str] = None
    """The version of codegen used to generate this workflow.

    .. deprecated::
        This field is deprecated. Use `runner_config.codegen_version` instead.
    """

    server_version: Optional[str] = None
    """The server version this workflow was generated for.

    .. deprecated::
        This field is deprecated. Use `runner_config.server_version` instead.
    """

    node_id_to_file_mapping: Dict[str, CodeResourceDefinition] = {}
    """Mapping of node IDs to their code resource definitions.

    Key: Node UUID string
    Value: CodeResourceDefinition containing the module path and class name
    """

    trigger_path_to_id_mapping: Dict[str, str] = {}
    """Mapping of trigger class paths to their UI trigger IDs.

    Key: ".<relative_trigger_module_path>.<TriggerClassName>"
    Value: UI trigger ID (UUID string)
    """

    trigger_attribute_id_mapping: Dict[str, str] = {}
    """Mapping of trigger attribute keys to their UI attribute IDs.

    Key: "<trigger_path>|<attribute_key>"
    Value: Attribute ID (UUID string)

    This ensures trigger attribute IDs remain stable across serialization
    round-trips.
    """

    entrypoint: Optional[str] = None
    """The UI entrypoint node ID.

    Kept for compatibility. Edge IDs for entrypoint flows are now included in
    edges_to_id_mapping using the manual trigger path as the source.
    """

    edges_to_id_mapping: Dict[str, str] = {}
    """Mapping of edge path keys to their UI edge IDs.

    Key: "<source_path>|<target_node_path>"
        where:
            source_path := "<trigger_module_path>.<TriggerClassName>" for trigger edges
                          or "<module_path>.<NodeClassName>.Ports.<source_handle_id>"
                             for node->node edges (handle id disambiguates multiple edges)
            target_node_path := "<module_path>.<ClassName>.Trigger" for the target node
    Value: UI edge ID (UUID string)
    """

    dataset_row_index_to_id_mapping: Dict[str, str] = {}
    """Mapping of dataset row indices to their UI row IDs.

    Key: Dataset row index (0-based) as a string
    Value: UI dataset row ID (UUID string)
    """
