#!/usr/bin/env python3
"""
🚀 智慧拓撲排序算法演示

這個演示展示了我們的進階pipeline拓撲分析和優化算法:
- 自動依賴關係分析
- 循環檢測和解決
- 並行執行優化
- 關鍵路徑分析
- 性能指標計算

適合進度報告展示！
"""

import json
from mflow_converter import MFlowConverter

def create_demo_pipeline() -> dict:
    """創建一個複雜的多階段pipeline用於演示"""
    return {
        "project_name": "Advanced Multi-Stage Fire Detection Pipeline",
        "description": "Demonstrates intelligent topology sorting with parallel stages",
        "nodes": [
            # Input Node
            {
                "id": "input_001",
                "name": "RGB Camera Input",
                "type": "ExactInputNode",
                "pos": [100, 200],
                "properties": {
                    "source_type": "Camera",
                    "device_id": 0,
                    "resolution": "1920x1080",
                    "fps": 30
                }
            },
            
            # Parallel Feature Extraction Stages
            {
                "id": "model_rgb_001", 
                "name": "RGB Feature Extractor",
                "type": "ExactModelNode",
                "pos": [300, 100],
                "properties": {
                    "model_path": "rgb_features.nef",
                    "scpu_fw_path": "fw_scpu.bin",
                    "ncpu_fw_path": "fw_ncpu.bin",
                    "dongle_series": "520",
                    "port_id": "28,30"
                }
            },
            
            {
                "id": "model_edge_002",
                "name": "Edge Feature Extractor", 
                "type": "ExactModelNode",
                "pos": [300, 200],
                "properties": {
                    "model_path": "edge_features.nef",
                    "scpu_fw_path": "fw_scpu.bin", 
                    "ncpu_fw_path": "fw_ncpu.bin",
                    "dongle_series": "520",
                    "port_id": "32,34"
                }
            },
            
            {
                "id": "model_thermal_003",
                "name": "Thermal Feature Extractor",
                "type": "ExactModelNode", 
                "pos": [300, 300],
                "properties": {
                    "model_path": "thermal_features.nef",
                    "scpu_fw_path": "fw_scpu.bin",
                    "ncpu_fw_path": "fw_ncpu.bin", 
                    "dongle_series": "520",
                    "port_id": "36,38"
                }
            },
            
            # Intermediate Processing Stages
            {
                "id": "model_fusion_004",
                "name": "Feature Fusion",
                "type": "ExactModelNode",
                "pos": [500, 150], 
                "properties": {
                    "model_path": "feature_fusion.nef",
                    "scpu_fw_path": "fw_scpu.bin",
                    "ncpu_fw_path": "fw_ncpu.bin",
                    "dongle_series": "720", 
                    "port_id": "40,42"
                }
            },
            
            {
                "id": "model_attention_005",
                "name": "Attention Mechanism",
                "type": "ExactModelNode",
                "pos": [500, 250],
                "properties": {
                    "model_path": "attention.nef", 
                    "scpu_fw_path": "fw_scpu.bin",
                    "ncpu_fw_path": "fw_ncpu.bin",
                    "dongle_series": "720",
                    "port_id": "44,46"
                }
            },
            
            # Final Classification Stage
            {
                "id": "model_classifier_006",
                "name": "Fire Classifier",
                "type": "ExactModelNode",
                "pos": [700, 200],
                "properties": {
                    "model_path": "fire_classifier.nef",
                    "scpu_fw_path": "fw_scpu.bin", 
                    "ncpu_fw_path": "fw_ncpu.bin",
                    "dongle_series": "720",
                    "port_id": "48,50"
                }
            },
            
            # Output Node
            {
                "id": "output_007",
                "name": "Detection Output",
                "type": "ExactOutputNode",
                "pos": [900, 200],
                "properties": {
                    "output_type": "Stream",
                    "format": "JSON",
                    "destination": "tcp://localhost:5555"
                }
            }
        ],
        
        "connections": [
            # Input to parallel feature extractors
            {"output_node": "input_001", "output_port": "output", "input_node": "model_rgb_001", "input_port": "input"},
            {"output_node": "input_001", "output_port": "output", "input_node": "model_edge_002", "input_port": "input"}, 
            {"output_node": "input_001", "output_port": "output", "input_node": "model_thermal_003", "input_port": "input"},
            
            # Feature extractors to fusion
            {"output_node": "model_rgb_001", "output_port": "output", "input_node": "model_fusion_004", "input_port": "input"},
            {"output_node": "model_edge_002", "output_port": "output", "input_node": "model_fusion_004", "input_port": "input"},
            {"output_node": "model_thermal_003", "output_port": "output", "input_node": "model_attention_005", "input_port": "input"},
            
            # Intermediate stages to classifier  
            {"output_node": "model_fusion_004", "output_port": "output", "input_node": "model_classifier_006", "input_port": "input"},
            {"output_node": "model_attention_005", "output_port": "output", "input_node": "model_classifier_006", "input_port": "input"},
            
            # Classifier to output
            {"output_node": "model_classifier_006", "output_port": "output", "input_node": "output_007", "input_port": "input"}
        ],
        
        "version": "1.0"
    }

def demo_simple_pipeline():
    """演示簡單的線性pipeline"""
    print("🎯 DEMO 1: Simple Linear Pipeline")
    print("="*50)
    
    simple_pipeline = {
        "project_name": "Simple Linear Pipeline",
        "nodes": [
            {"id": "model_001", "name": "Detection", "type": "ExactModelNode", "properties": {"model_path": "detect.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "28"}},
            {"id": "model_002", "name": "Classification", "type": "ExactModelNode", "properties": {"model_path": "classify.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "30"}},
            {"id": "model_003", "name": "Verification", "type": "ExactModelNode", "properties": {"model_path": "verify.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "32"}}
        ],
        "connections": [
            {"output_node": "model_001", "input_node": "model_002"},
            {"output_node": "model_002", "input_node": "model_003"}
        ]
    }
    
    converter = MFlowConverter()
    config = converter._convert_mflow_to_config(simple_pipeline)
    print("\n")

def demo_parallel_pipeline():
    """演示並行pipeline"""
    print("🎯 DEMO 2: Parallel Processing Pipeline") 
    print("="*50)
    
    parallel_pipeline = {
        "project_name": "Parallel Processing Pipeline",
        "nodes": [
            {"id": "model_001", "name": "RGB Processor", "type": "ExactModelNode", "properties": {"model_path": "rgb.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "28"}},
            {"id": "model_002", "name": "IR Processor", "type": "ExactModelNode", "properties": {"model_path": "ir.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "30"}},
            {"id": "model_003", "name": "Depth Processor", "type": "ExactModelNode", "properties": {"model_path": "depth.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "32"}},
            {"id": "model_004", "name": "Fusion Engine", "type": "ExactModelNode", "properties": {"model_path": "fusion.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "34"}}
        ],
        "connections": [
            {"output_node": "model_001", "input_node": "model_004"},
            {"output_node": "model_002", "input_node": "model_004"}, 
            {"output_node": "model_003", "input_node": "model_004"}
        ]
    }
    
    converter = MFlowConverter()
    config = converter._convert_mflow_to_config(parallel_pipeline)
    print("\n")

def demo_complex_pipeline():
    """演示複雜的多層級pipeline"""
    print("🎯 DEMO 3: Complex Multi-Level Pipeline")
    print("="*50)
    
    complex_pipeline = create_demo_pipeline()
    
    converter = MFlowConverter()
    config = converter._convert_mflow_to_config(complex_pipeline)
    
    # 顯示額外的配置信息
    print("🔧 Generated Pipeline Configuration:")
    print(f"   • Stage Configs: {len(config.stage_configs)}")
    print(f"   • Input Config: {config.input_config.get('source_type', 'Unknown')}")
    print(f"   • Output Config: {config.output_config.get('format', 'Unknown')}")
    print("\n")

def demo_cycle_detection():
    """演示循環檢測和解決"""
    print("🎯 DEMO 4: Cycle Detection & Resolution")
    print("="*50)
    
    # 創建一個有循環的pipeline
    cycle_pipeline = {
        "project_name": "Pipeline with Cycles (Testing)", 
        "nodes": [
            {"id": "model_A", "name": "Model A", "type": "ExactModelNode", "properties": {"model_path": "a.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "28"}},
            {"id": "model_B", "name": "Model B", "type": "ExactModelNode", "properties": {"model_path": "b.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "30"}},
            {"id": "model_C", "name": "Model C", "type": "ExactModelNode", "properties": {"model_path": "c.nef", "scpu_fw_path": "fw_scpu.bin", "ncpu_fw_path": "fw_ncpu.bin", "port_id": "32"}}
        ],
        "connections": [
            {"output_node": "model_A", "input_node": "model_B"},
            {"output_node": "model_B", "input_node": "model_C"},
            {"output_node": "model_C", "input_node": "model_A"}  # Creates cycle!
        ]
    }
    
    converter = MFlowConverter()
    config = converter._convert_mflow_to_config(cycle_pipeline)
    print("\n")

def demo_performance_analysis():
    """演示性能分析功能"""
    print("🎯 DEMO 5: Performance Analysis")
    print("="*50)
    
    # 使用之前創建的複雜pipeline
    complex_pipeline = create_demo_pipeline()
    
    converter = MFlowConverter()
    config = converter._convert_mflow_to_config(complex_pipeline)
    
    # 驗證配置
    is_valid, errors = converter.validate_config(config)
    
    print("🔍 Configuration Validation:")
    if is_valid:
        print("   ✅ All configurations are valid!")
    else:
        print("   ⚠️  Configuration issues found:")
        for error in errors[:3]:  # Show first 3 errors
            print(f"      - {error}")
    
    print(f"\n📦 Ready for InferencePipeline Creation:")
    print(f"   • Total Stages: {len(config.stage_configs)}")
    print(f"   • Pipeline Name: {config.pipeline_name}")
    print(f"   • Preprocessing Configs: {len(config.preprocessing_configs)}")
    print(f"   • Postprocessing Configs: {len(config.postprocessing_configs)}")
    print("\n")

def main():
    """主演示函數"""
    print("🚀 INTELLIGENT PIPELINE TOPOLOGY SORTING DEMONSTRATION")
    print("="*60)
    print("This demo showcases our advanced pipeline analysis capabilities:")
    print("• Automatic dependency resolution")
    print("• Parallel execution optimization") 
    print("• Cycle detection and prevention")
    print("• Critical path analysis")
    print("• Performance metrics calculation")
    print("="*60 + "\n")
    
    try:
        # 運行所有演示
        demo_simple_pipeline()
        demo_parallel_pipeline() 
        demo_complex_pipeline()
        demo_cycle_detection()
        demo_performance_analysis()
        
        print("🎉 ALL DEMONSTRATIONS COMPLETED SUCCESSFULLY!")
        print("Ready for production deployment and progress reporting! 🚀")
        
    except Exception as e:
        print(f"❌ Demo error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    main()