#!/usr/bin/env python3
"""
Test script to verify PipelineCapture configuration.
"""

import json
from evileye.core.logging_config import setup_evileye_logging
from evileye.core.logger import get_module_logger
import os

# Инициализация логирования для тестов
logger = setup_evileye_logging(log_level="INFO", log_to_console=True, log_to_file=True)
test_logger = get_module_logger("test")

def test_pipeline_capture_config():
    """Test PipelineCapture configuration loading."""
    
    test_logger.info("🔍 Testing PipelineCapture Configuration")
    test_logger.info("=" * 60)
    
    try:
        # Load configuration
        config_path = "evileye/samples_configs/pipeline_capture.json"
        with open(config_path, 'r') as f:
            config = json.load(f)
        
        test_logger.info("✅ Configuration loaded successfully")
        
        # Check pipeline section
        assert 'pipeline' in config
        assert config['pipeline']['pipeline_class'] == 'PipelineCapture'
        test_logger.info("✅ Pipeline section correct")
        
        # Check sources section
        assert 'sources' in config['pipeline']
        assert len(config['pipeline']['sources']) == 1
        source = config['pipeline']['sources'][0]
        assert 'source' in source
        assert 'fps' in source
        assert source['fps']['value'] == 30
        test_logger.info("✅ Sources section correct")
        
        # Check controller section
        assert 'controller' in config
        assert config['controller']['fps'] == 30
        assert config['controller']['use_database'] == False
        test_logger.info("✅ Controller section correct")
        
        # Check other sections
        assert 'objects_handler' in config
        assert 'events_detectors' in config
        assert 'database' in config
        assert 'visualizer' in config
        test_logger.info("✅ All required sections present")
        
        # Check visualizer text_config
        assert 'text_config' in config['visualizer']
        text_config = config['visualizer']['text_config']
        assert 'font_size_pt' in text_config
        assert 'font_face' in text_config
        assert 'color' in text_config
        test_logger.info("✅ Text configuration present")
        
        test_logger.info("✅ PipelineCapture configuration test completed successfully")
        
    except Exception as e:
        test_logger.error(f"❌ Error in configuration test: {e}")
        import traceback
        traceback.print_exc()

def test_pipeline_capture_usage():
    """Test PipelineCapture usage with configuration."""
    
    test_logger.info("\n🔍 Testing PipelineCapture Usage")
    test_logger.info("=" * 60)
    
    try:
        from evileye.pipelines.pipeline_capture import PipelineCapture
        
        # Create pipeline
        pipeline = PipelineCapture()
        test_logger.info("✅ PipelineCapture created")
        
        # Load configuration
        config_path = "evileye/samples_configs/pipeline_capture.json"
        with open(config_path, 'r') as f:
            config = json.load(f)
        
        # Set pipeline parameters
        pipeline.params = config['pipeline']
        pipeline.set_params_impl()
        
        assert pipeline.video_path == "videos/sample_video.mp4"
        assert pipeline.fps == 30
        test_logger.info("✅ Configuration applied successfully")
        
        # Test default structure generation
        default_structure = pipeline.generate_default_structure(1)
        assert 'pipeline' in default_structure
        assert default_structure['pipeline']['pipeline_class'] == 'PipelineCapture'
        test_logger.info("✅ Default structure generation working")
        
        test_logger.info("✅ PipelineCapture usage test completed successfully")
        
    except Exception as e:
        test_logger.error(f"❌ Error in usage test: {e}")
        import traceback
        traceback.print_exc()

def main():
    """Main test function."""
    
    test_logger.info("🔍 PipelineCapture Configuration Test")
    test_logger.info("=" * 60)
    
    test_pipeline_capture_config()
    test_pipeline_capture_usage()
    
    test_logger.info("\n📋 Summary:")
    test_logger.info("  ✅ Configuration file structure correct")
    test_logger.info("  ✅ All required sections present")
    test_logger.info("  ✅ PipelineCapture can load configuration")
    test_logger.info("  ✅ Default structure generation working")
    test_logger.info("  ✅ All tests passed successfully")

if __name__ == "__main__":
    main()



