#!/usr/bin/env python3
"""
Default Mainline Visualization

Renders the standard Mainline visualization using the graph-based DSL.
This demonstrates the default behavior: headlines source, scroll camera,
terminal display, with classic effects (noise, fade, glitch, firehose).

Usage:
    python examples/default_visualization.py

The visualization will be rendered once and printed to stdout.
"""

import sys
from pathlib import Path

# Add the project root to Python path
sys.path.insert(0, str(Path(__file__).parent.parent))

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


def main():
    """Render the default Mainline visualization."""
    print("Loading default Mainline visualization...")
    print("=" * 70)

    # Discover effect plugins
    discover_plugins()

    # Path to the TOML configuration
    toml_path = Path(__file__).parent / "default_visualization.toml"

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

    # Load pipeline from TOML configuration
    try:
        pipeline = load_pipeline_from_toml(
            toml_path, viewport_width=80, viewport_height=24
        )
        print(f"✓ Pipeline loaded from {toml_path.name}")
        print(f"  Stages: {list(pipeline._stages.keys())}")
    except Exception as e:
        print(f"Error loading pipeline: {e}", file=sys.stderr)
        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")

    # Set up execution context
    ctx = pipeline.context
    ctx.terminal_width = 80
    ctx.terminal_height = 24

    # Create params for the execution
    params = PipelineParams(viewport_width=80, viewport_height=24)
    ctx.params = params

    # Execute the pipeline (empty items list - source will provide content)
    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()
