#!/usr/bin/env python3
"""
Test script to verify graph-based pipeline integration.

This script tests that the graph DSL can be used to create working pipelines
that produce output similar to preset-based pipelines.
"""

from engine.effects.plugins import discover_plugins
from engine.pipeline.graph_toml import load_pipeline_from_toml
from engine.pipeline.params import PipelineParams


def test_graph_pipeline_execution():
    """Test that a graph-based pipeline can execute and produce output."""
    print("=== Testing Graph Pipeline Execution ===")

    # Discover plugins
    discover_plugins()

    # Load pipeline from TOML
    pipeline = load_pipeline_from_toml(
        "examples/pipeline_graph.toml", viewport_width=80, viewport_height=24
    )

    print(f"Pipeline loaded with {len(pipeline._stages)} stages")
    print(f"Stages: {list(pipeline._stages.keys())}")

    # Initialize pipeline
    if not pipeline.initialize():
        print("Failed to initialize pipeline")
        return False

    print("Pipeline initialized successfully")

    # Set up context
    ctx = pipeline.context
    params = PipelineParams(viewport_width=80, viewport_height=24)
    ctx.params = params

    # Execute pipeline with empty items (source will provide content)
    result = pipeline.execute([])

    if result.success:
        print(f"Pipeline executed successfully")
        print(f"Output type: {type(result.data)}")
        if isinstance(result.data, list):
            print(f"Output lines: {len(result.data)}")
            if len(result.data) > 0:
                print(f"First line: {result.data[0][:50]}...")
        return True
    else:
        print(f"Pipeline execution failed: {result.error}")
        return False


def test_graph_vs_preset():
    """Compare graph-based and preset-based pipelines."""
    print("\n=== Comparing Graph vs Preset ===")

    from engine.pipeline import get_preset

    # Load graph-based pipeline
    graph_pipeline = load_pipeline_from_toml(
        "examples/pipeline_graph.toml", viewport_width=80, viewport_height=24
    )

    # Load preset-based pipeline (using test-basic as a base)
    preset = get_preset("test-basic")
    if not preset:
        print("test-basic preset not found")
        return False

    # Create pipeline from preset config
    from engine.pipeline import Pipeline

    preset_pipeline = Pipeline(config=preset.to_config())

    print(f"Graph pipeline stages: {len(graph_pipeline._stages)}")
    print(f"Preset pipeline stages: {len(preset_pipeline._stages)}")

    # Compare stage types
    graph_stage_types = {
        name: stage.__class__.__name__ for name, stage in graph_pipeline._stages.items()
    }
    preset_stage_types = {
        name: stage.__class__.__name__
        for name, stage in preset_pipeline._stages.items()
    }

    print("\nGraph pipeline stages:")
    for name, stage_type in graph_stage_types.items():
        print(f"  - {name}: {stage_type}")

    print("\nPreset pipeline stages:")
    for name, stage_type in preset_stage_types.items():
        print(f"  - {name}: {stage_type}")

    return True


if __name__ == "__main__":
    success1 = test_graph_pipeline_execution()
    success2 = test_graph_vs_preset()

    if success1 and success2:
        print("\n✓ All tests passed!")
    else:
        print("\n✗ Some tests failed")
        exit(1)
