"""Wright pipeline workflow — generates 4-object DDL pipelines.

Reuses:
    - ``src.wright.pipeline_generator.MartPipelineGenerator``
    - ``src.wright.types.MartConfig``
"""

from __future__ import annotations

from typing import Any, Dict, List, Optional

from databridge_core.sdk.core.context import SDKContext
from databridge_core.sdk.core.exceptions import NotImplementedStubError
from databridge_core.sdk.core.types import CompiledArtifact, Result
from databridge_core.sdk.workflows.base import BaseWorkflow
from databridge_core.sdk.workflows.result import WorkflowResult
from databridge_core.sdk.workflows.step import workflow_step


class WrightPipelineWorkflow(BaseWorkflow):
    """Generate a Wright 4-object DDL pipeline (VW_1, DT_2, DT_3A, DT_3).

    Converts a hierarchy project into a deployable Snowflake pipeline.

    Reuses:
        - ``src.wright.pipeline_generator.MartPipelineGenerator``
        - ``src.wright.types.MartConfig``
    """

    name = "hierarchy_reporting.wright_pipeline"
    category = "hierarchy_reporting"
    description = "Generate Wright 4-object DDL pipeline from hierarchy"
    required_params = ["project_id"]
    optional_params = {"output_dir": "data/mart_pipelines", "target_schema": None}

    def validate(self, params: Dict[str, Any]) -> Dict[str, Any]:
        self._check_required(params)
        return self._apply_defaults(params)

    def execute(
        self, params: Dict[str, Any], ctx: Optional[SDKContext] = None
    ) -> Result:
        steps = [
            self._build_config(params, ctx),
            self._generate_pipeline(params, ctx),
        ]
        # Graph integration
        if ctx and ctx.graphs.available:
            steps.append(self._graph_record_lineage(params, ctx))
            steps.append(self._graph_index_vectors(params, ctx))
        return WorkflowResult.from_steps(self.name, steps)

    def compile_to_snowflake(
        self, params: Dict[str, Any], ctx: Optional[SDKContext] = None
    ) -> List[CompiledArtifact]:
        # REUSE: MartPipelineGenerator(output_dir).generate_vw1(config),
        #        .generate_dt2(config), .generate_dt3a(config), .generate_dt3(config)
        # Then wrap each PipelineObject.ddl into CompiledArtifact
        raise NotImplementedStubError(
            self.name,
            "compile_to_snowflake",
            "src.wright.pipeline_generator.MartPipelineGenerator.generate_vw1/dt2/dt3a/dt3(config)",
        )

    @workflow_step("build_config", "Build MartConfig from hierarchy project")
    def _build_config(self, params: Dict[str, Any], ctx: Optional[SDKContext] = None):
        # REUSE: Load project via HierarchyService.get_project(project_id)
        #        Convert to src.wright.types.MartConfig
        raise NotImplementedStubError(
            self.name,
            "build_config",
            "src.hierarchy.service.HierarchyService.get_project() -> src.wright.types.MartConfig",
        )

    @workflow_step("generate_pipeline", "Generate 4-object DDL pipeline")
    def _generate_pipeline(self, params: Dict[str, Any], ctx: Optional[SDKContext] = None):
        # REUSE: MartPipelineGenerator(output_dir=params["output_dir"])
        #        .generate_vw1(config), .generate_dt2(config), etc.
        raise NotImplementedStubError(
            self.name,
            "generate_pipeline",
            "src.wright.pipeline_generator.MartPipelineGenerator.generate_vw1/dt2/dt3a/dt3()",
        )

    # --- Graph integration ---

    @workflow_step("graph_record_lineage", "Record VW→DT chain edges in lineage")
    def _graph_record_lineage(self, params: Dict[str, Any], ctx: Optional[SDKContext] = None):
        # ctx.graphs.record_lineage_node("sdk", "VW_1", "VIEW")
        # ctx.graphs.record_lineage_node("sdk", "DT_2", "DYNAMIC_TABLE")
        # ctx.graphs.record_lineage_node("sdk", "DT_3A", "DYNAMIC_TABLE")
        # ctx.graphs.record_lineage_node("sdk", "DT_3", "DYNAMIC_TABLE")
        # ctx.graphs.record_lineage_edge("sdk", "VW_1", "DT_2", "DIRECT")
        # ctx.graphs.record_lineage_edge("sdk", "DT_2", "DT_3A", "AGGREGATION")
        # ctx.graphs.record_lineage_edge("sdk", "DT_3A", "DT_3", "DIRECT")
        raise NotImplementedStubError(
            self.name,
            "graph_record_lineage",
            "ctx.graphs.record_lineage_node/edge — VW_1→DT_2→DT_3A→DT_3 chain",
        )

    @workflow_step("graph_index_vectors", "Index pipeline description in vector store")
    def _graph_index_vectors(self, params: Dict[str, Any], ctx: Optional[SDKContext] = None):
        # ctx.graphs.index_content(
        #     doc_id=f"wright_{project_id}", content=pipeline_description,
        #     source_type="wright_pipeline", metadata={...},
        # )
        raise NotImplementedStubError(
            self.name,
            "graph_index_vectors",
            "ctx.graphs.index_content(doc_id, content, 'wright_pipeline', metadata)",
        )
