#!/usr/bin/env python
import os
import sys
import django

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from reports.comprehensive_reports import reports_service
from django.db import connection

print("=== TESTING REPORTS SERVICE ===")

try:
    # Test the collation setting
    with connection.cursor() as cursor:
        cursor.execute("SET collation_connection = 'utf8mb4_unicode_ci'")
    print("✓ Collation set successfully")
    
    # Test the reports service
    print("\nTesting reports_service.generate_comprehensive_dashboard_data()...")
    dashboard_data = reports_service.generate_comprehensive_dashboard_data()
    
    print("✓ Reports service executed successfully")
    print(f"\nSummary metrics:")
    summary = dashboard_data.get('summary_metrics', {})
    for key, value in summary.items():
        print(f"  {key}: {value}")
        
except Exception as e:
    print(f"✗ ERROR in reports service: {e}")
    print(f"Error type: {type(e).__name__}")
    import traceback
    print(f"\nFull traceback:")
    traceback.print_exc()

print("\n=== END TEST ===")