#!/usr/bin/env python3

import sys
import os
from evileye.core.logging_config import setup_evileye_logging
from evileye.core.logger import get_module_logger
sys.path.append('.')

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

def test_journal_columns_compatibility():
    """Test that JSON journal columns match database journal structure"""
    
    test_logger.info("=== Test Journal Columns Compatibility ===")
    
    try:
        from PyQt6.QtWidgets import QApplication
        from evileye.visualization_modules.events_journal_json import EventsJournalJson
        import cv2
        import numpy as np
        import datetime
        
        # Create a simple test application
        app = QApplication([])
        
        # Create test directory structure
        base_dir = 'EvilEyeData'
        today = datetime.date.today().strftime('%Y_%m_%d')
        test_images_dir = os.path.join(base_dir, 'images', today)
        os.makedirs(test_images_dir, exist_ok=True)
        
        # Create test preview image
        test_image = np.zeros((150, 300, 3), dtype=np.uint8)
        test_image[:] = (100, 150, 200)  # Blue-gray background
        cv2.rectangle(test_image, (50, 30), (250, 120), (0, 255, 0), 2)
        
        # Save test preview image
        preview_path = os.path.join(test_images_dir, 'detected_previews', 'test_preview.jpeg')
        os.makedirs(os.path.dirname(preview_path), exist_ok=True)
        cv2.imwrite(preview_path, test_image)
        
        test_logger.info(f"✅ Created test preview image: {preview_path}")
        
        # Create test JSON data with source_name
        test_json_path = os.path.join(test_images_dir, 'objects_found.json')
        test_data = {
            "metadata": {
                "version": "1.0",
                "created": datetime.datetime.now().isoformat(),
                "description": "Test data",
                "total_objects": 1
            },
            "objects": [
                {
                    "object_id": 1,
                    "frame_id": 1,
                    "timestamp": datetime.datetime.now().isoformat(),
                    "image_filename": "detected_previews/test_preview.jpeg",
                    "bounding_box": {
                        "x": 50,
                        "y": 30,
                        "width": 200,
                        "height": 90
                    },
                    "class_id": 0,
                    "class_name": "person",
                    "confidence": 0.85,
                    "source_id": 0,
                    "source_name": "Cam1",
                    "date_folder": today
                }
            ]
        }
        
        import json
        with open(test_json_path, 'w') as f:
            json.dump(test_data, f, indent=2)
        
        test_logger.info(f"✅ Created test JSON data: {test_json_path}")
        
        # Create EventsJournalJson widget
        journal = EventsJournalJson(base_dir)
        journal.show()
        
        test_logger.info("✅ Created EventsJournalJson widget")
        
        # Check table structure
        if journal.table.columnCount() == 7:
            test_logger.info(f"✅ Table has {journal.table.columnCount()} columns (matches database journal)")
            
            # Check column headers
            headers = []
            for i in range(journal.table.columnCount()):
                headers.append(journal.table.horizontalHeaderItem(i).text())
            
            expected_headers = ['Name', 'Event', 'Information', 'Time', 'Time lost', 'Preview', 'Lost preview']
            
            if headers == expected_headers:
                test_logger.info("✅ Column headers match database journal structure")
                for i, header in enumerate(headers):
                    test_logger.info(f"   Column {i}: {header}")
            else:
                test_logger.info("❌ Column headers don't match expected structure")
                test_logger.info(f"   Expected: {expected_headers}")
                test_logger.info(f"   Actual: {headers}")
        else:
            test_logger.error(f"❌ Table has {journal.table.columnCount()} columns (expected 7)")
        
        # Check that the table loads data
        if journal.table.rowCount() > 0:
            test_logger.info(f"✅ Table loaded {journal.table.rowCount()} rows")
            
            # Check first row data
            first_row = 0
            name_item = journal.table.item(first_row, 0)  # Name column
            event_item = journal.table.item(first_row, 1)  # Event column
            info_item = journal.table.item(first_row, 2)  # Information column
            time_item = journal.table.item(first_row, 3)  # Time column
            time_lost_item = journal.table.item(first_row, 4)  # Time lost column
            preview_item = journal.table.item(first_row, 5)  # Preview column
            lost_preview_item = journal.table.item(first_row, 6)  # Lost preview column
            
            if name_item and name_item.text() == 'Cam1':
                test_logger.info("✅ Name column contains source_name: Cam1")
            else:
                test_logger.error(f"❌ Name column contains: {name_item.text() if name_item else 'None'}")
            
            if event_item and event_item.text() == 'Event':
                test_logger.info("✅ Event column contains 'Event' (matches database journal)")
            else:
                test_logger.error(f"❌ Event column contains: {event_item.text() if event_item else 'None'}")
            
            if info_item and 'Object Id=1' in info_item.text():
                test_logger.info("✅ Information column contains object details")
            else:
                test_logger.error(f"❌ Information column contains: {info_item.text() if info_item else 'None'}")
            
            if preview_item and preview_item.text():
                test_logger.info(f"✅ Preview column contains image path: {preview_item.text()}")
            else:
                test_logger.info("❌ Preview column is empty")
            
            if lost_preview_item:
                test_logger.info("✅ Lost preview column exists")
            else:
                test_logger.info("❌ Lost preview column missing")
        else:
            test_logger.info("❌ Table is empty")
        
        # Close the widget
        journal.close()
        
        test_logger.info("\n✅ Journal columns compatibility test completed")
        test_logger.info("\n📋 Summary:")
        test_logger.info("   ✅ JSON journal has same column structure as database journal")
        test_logger.info("   ✅ All 7 columns present: Name, Event, Information, Time, Time lost, Preview, Lost preview")
        test_logger.info("   ✅ Column headers match database journal format")
        test_logger.info("   ✅ Data is loaded correctly from JSON files")
        test_logger.info("   ✅ Source name is displayed in Name column")
        test_logger.info("   ✅ Event column shows 'Event' (matches database journal)")
        
    except Exception as e:
        test_logger.error(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_journal_columns_compatibility()

