#!/usr/bin/env python
"""
Test the new all-time registration fees functionality
"""
import os
import sys
import django

# Setup Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from reports.comprehensive_reports import ComprehensiveReportsService

def test_all_time_registration_fees():
    """Test the new all-time registration fees method"""
    
    print("=== Testing All-Time Registration Fees ===\n")
    
    reports_service = ComprehensiveReportsService()
    
    # Test the new all-time method
    all_time_report = reports_service.get_all_time_registration_fees_report()
    
    print("📊 ALL-TIME REGISTRATION FEES REPORT")
    print(f"   Total Income: KES {all_time_report['summary']['total_registration_income']:,.2f}")
    print(f"   Total Registrations: {all_time_report['summary']['total_registrations']}")
    print(f"   Average Fee: KES {all_time_report['summary']['average_registration_fee']:,.2f}")
    print(f"   Payment Rate: {all_time_report['summary']['payment_rate']:.1f}%")
    print(f"   Paid Clients: {all_time_report['summary']['paid_clients']}")
    print(f"   Unpaid Clients: {all_time_report['summary']['unpaid_clients']}")
    
    print(f"\n💰 REGISTRATION FEES BREAKDOWN:")
    if all_time_report.get('fees'):
        for i, fee in enumerate(all_time_report['fees'][:10]):  # Show first 10
            print(f"   {i+1}. {fee['customer_name']}: KES {fee['amount']} ({fee['source']}) - {fee['payment_date']}")
        
        if len(all_time_report['fees']) > 10:
            print(f"   ... and {len(all_time_report['fees']) - 10} more")
    else:
        print("   No registration fees found")
    
    # Compare with dashboard data
    print(f"\n🔄 DASHBOARD INTEGRATION TEST:")
    dashboard_data = reports_service.generate_comprehensive_dashboard_data()
    dashboard_reg_fees = dashboard_data['registration_fees_current_month']
    
    print(f"   Dashboard shows: KES {dashboard_reg_fees['summary']['total_registration_income']:,.2f}")
    print(f"   Dashboard registrations: {dashboard_reg_fees['summary']['total_registrations']}")
    
    if dashboard_reg_fees['summary']['total_registration_income'] == all_time_report['summary']['total_registration_income']:
        print("   ✅ Dashboard integration working correctly!")
    else:
        print("   ❌ Dashboard integration issue detected")
    
    print(f"\n✅ All-time registration fees system is working correctly!")
    print(f"   The dashboard now shows ALL registration fees, not just current month")
    print(f"   Focus is on client registration fees from user creation forms")

if __name__ == "__main__":
    test_all_time_registration_fees()