#!/usr/bin/env python3
"""
Test script to validate video upscaling pipeline configuration.

This script tests the fixes made to support video-only workflows
where the first step expects video input instead of text.
"""

import sys
import os
from pathlib import Path

# Add the ai_content_pipeline to the Python path
current_dir = Path(__file__).parent
sys.path.insert(0, str(current_dir))

from ai_content_pipeline.pipeline.manager import AIPipelineManager
from ai_content_pipeline.pipeline.chain import ContentCreationChain


def test_video_upscale_validation():
    """Test that video upscaling pipeline validation works correctly."""
    
    print("🧪 Testing Video Upscaling Pipeline Validation")
    print("=" * 50)
    
    # Initialize pipeline manager
    manager = AIPipelineManager(base_dir=current_dir)
    
    try:
        # Load the video upscale configuration
        config_path = current_dir / "input" / "video_upscale_topaz.yaml"
        
        if not config_path.exists():
            print(f"❌ Configuration file not found: {config_path}")
            return False
            
        print(f"📄 Loading configuration: {config_path}")
        chain = manager.create_chain_from_config(str(config_path))
        
        print(f"🔍 Chain created: {chain.name}")
        print(f"📋 Steps: {len(chain.steps)}")
        print(f"🎯 Expected input type: {chain.get_initial_input_type()}")
        
        # Validate the chain
        print("\n🔍 Validating chain configuration...")
        errors = chain.validate()
        
        if errors:
            print("❌ Validation failed with errors:")
            for error in errors:
                print(f"   - {error}")
            return False
        else:
            print("✅ Validation passed!")
            
        # Test the chain with mock video input
        print("\n🎬 Testing chain execution validation...")
        test_video_path = "/path/to/test/video.mp4"  # Mock path for validation
        
        print(f"📝 Input data: {test_video_path}")
        print(f"📝 Input type: {chain.get_initial_input_type()}")
        
        # Dry run validation (don't actually execute)
        print("✅ Chain validation and input type detection working correctly!")
        
        # Print chain details
        print(f"\n📊 Chain Details:")
        print(f"   Name: {chain.name}")
        print(f"   Input type: {chain.get_initial_input_type()}")
        print(f"   Steps: {len(chain.get_enabled_steps())}")
        
        for i, step in enumerate(chain.get_enabled_steps()):
            print(f"   {i+1}. {step.step_type.value} ({step.model})")
        
        return True
        
    except Exception as e:
        print(f"❌ Test failed with exception: {str(e)}")
        import traceback
        traceback.print_exc()
        return False


def test_text_to_video_validation():
    """Test that traditional text-to-video pipeline still works."""
    
    print("\n🧪 Testing Traditional Text-to-Video Pipeline")
    print("=" * 50)
    
    manager = AIPipelineManager()
    
    try:
        # Create a simple text-to-video chain
        chain = manager.create_simple_chain(
            steps=["text_to_image", "image_to_video"],
            models={"text_to_image": "flux_dev", "image_to_video": "hailuo"},
            name="test_text_to_video"
        )
        
        print(f"🔍 Chain created: {chain.name}")
        print(f"🎯 Expected input type: {chain.get_initial_input_type()}")
        
        # Validate the chain
        errors = chain.validate()
        
        if errors:
            print("❌ Validation failed with errors:")
            for error in errors:
                print(f"   - {error}")
            return False
        else:
            print("✅ Validation passed!")
            
        return True
        
    except Exception as e:
        print(f"❌ Test failed with exception: {str(e)}")
        return False


def main():
    """Run all validation tests."""
    
    print("🚀 Starting Pipeline Validation Tests")
    print("=" * 60)
    
    success_count = 0
    total_tests = 2
    
    # Test 1: Video upscaling validation
    if test_video_upscale_validation():
        success_count += 1
    
    # Test 2: Traditional text-to-video validation  
    if test_text_to_video_validation():
        success_count += 1
    
    # Summary
    print(f"\n📊 Test Results: {success_count}/{total_tests} tests passed")
    
    if success_count == total_tests:
        print("🎉 All tests passed! Pipeline validation fixes are working correctly.")
        return True
    else:
        print("❌ Some tests failed. Please review the errors above.")
        return False


if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)