#!/usr/bin/env python3
"""
Hybrid Preset-Graph Visualization

Demonstrates the new hybrid configuration format that combines
preset simplicity with graph flexibility.

This uses 70% less space than the verbose node-based DSL while
providing the same functionality.

Usage:
    python examples/hybrid_visualization.py
"""

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from engine.effects.plugins import discover_plugins
from engine.pipeline.hybrid_config import load_hybrid_config


def main():
    """Render visualization using hybrid configuration."""
    print("Loading hybrid configuration...")
    print("=" * 70)

    # Discover effect plugins
    discover_plugins()

    # Path to the hybrid configuration
    toml_path = Path(__file__).parent / "hybrid_config.toml"

    if not toml_path.exists():
        print(f"Error: Configuration file not found: {toml_path}", file=sys.stderr)
        sys.exit(1)

    # Load hybrid configuration
    try:
        config = load_hybrid_config(toml_path)
        print(f"✓ Hybrid config loaded from {toml_path.name}")
        print(f"  Source: {config.source}")
        print(f"  Camera: {config.camera.mode if config.camera else 'none'}")
        print(f"  Effects: {len(config.effects)}")
        for effect in config.effects:
            print(f"    - {effect.name}: intensity={effect.intensity}")
        print(f"  Display: {config.display.backend if config.display else 'terminal'}")
    except Exception as e:
        print(f"Error loading config: {e}", file=sys.stderr)
        import traceback

        traceback.print_exc()
        sys.exit(1)

    # Convert to pipeline
    try:
        pipeline = config.to_pipeline(
            viewport_width=config.viewport_width, viewport_height=config.viewport_height
        )
        print(f"✓ Pipeline created with {len(pipeline._stages)} stages")
        print(f"  Stages: {list(pipeline._stages.keys())}")
    except Exception as e:
        print(f"Error creating pipeline: {e}", file=sys.stderr)
        import traceback

        traceback.print_exc()
        sys.exit(1)

    # Initialize the pipeline
    if not pipeline.initialize():
        print("Error: Failed to initialize pipeline", file=sys.stderr)
        sys.exit(1)
    print("✓ Pipeline initialized")

    # Execute the pipeline
    print("Executing pipeline...")
    result = pipeline.execute([])

    # Render output
    if result.success:
        print("=" * 70)
        print("Visualization Output:")
        print("=" * 70)
        for i, line in enumerate(result.data):
            print(line)
        print("=" * 70)
        print(f"✓ Successfully rendered {len(result.data)} lines")
    else:
        print(f"Error: Pipeline execution failed: {result.error}", file=sys.stderr)
        sys.exit(1)


if __name__ == "__main__":
    main()
