"""
Test script for Registration Fees Report
"""
import os
import django

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

from reports.simple_reports_service import SimpleReportsService
from users.models import CustomUser
from decimal import Decimal

def test_registration_fees_report():
    """Test the registration fees report"""
    print("=" * 80)
    print("TESTING REGISTRATION FEES REPORT")
    print("=" * 80)
    
    # Initialize service
    service = SimpleReportsService()
    
    # Check if we have any users with registration fees
    users_with_fees = CustomUser.objects.filter(
        role='borrower',
        registration_fee_amount__gt=0
    ).exclude(registration_fee_amount__isnull=True)
    
    print(f"\n✓ Total users with registration fees: {users_with_fees.count()}")
    
    paid_users = users_with_fees.filter(registration_fee_paid=True)
    print(f"✓ Paid registration fees: {paid_users.count()}")
    
    pending_users = users_with_fees.filter(registration_fee_paid=False)
    print(f"✓ Pending registration fees: {pending_users.count()}")
    
    # Test the report
    print("\n" + "=" * 80)
    print("TESTING REPORT DATA")
    print("=" * 80)
    
    report_data = service.get_registration_fees_report()
    
    print(f"\n✓ Report Type: {report_data.get('report_type')}")
    print(f"✓ Period: {report_data.get('period', {}).get('period_display')}")
    
    summary = report_data.get('summary', {})
    print(f"\n✓ Total Registration Income: KSh {summary.get('total_registration_income', 0):,.2f}")
    print(f"✓ Total Registrations: {summary.get('total_registrations', 0)}")
    print(f"✓ Average Fee: KSh {summary.get('average_registration_fee', 0):,.2f}")
    print(f"✓ Collection Rate: {summary.get('collection_rate', 0):.1f}%")
    print(f"✓ Paid Registrations: {summary.get('paid_registrations', 0)}")
    print(f"✓ Pending Payments: KSh {summary.get('pending_payments', 0):,.2f}")
    print(f"✓ Fees Collected: KSh {summary.get('fees_collected', 0):,.2f}")
    print(f"✓ Fees Outstanding: KSh {summary.get('fees_outstanding', 0):,.2f}")
    
    fees = report_data.get('fees', [])
    registrations = report_data.get('registrations', [])
    
    print(f"\n✓ Number of fee records: {len(fees)}")
    print(f"✓ Number of registration records: {len(registrations)}")
    
    if registrations:
        print("\n" + "=" * 80)
        print("SAMPLE REGISTRATION RECORDS (First 5)")
        print("=" * 80)
        
        for i, reg in enumerate(registrations[:5], 1):
            print(f"\n{i}. {reg.get('client_name')}")
            print(f"   Phone: {reg.get('client_phone')}")
            print(f"   Registration Date: {reg.get('registration_date')}")
            print(f"   Fee Amount: KSh {reg.get('fee_amount', 0):,.2f}")
            print(f"   Payment Status: {reg.get('payment_status')}")
            print(f"   Payment Method: {reg.get('payment_method')}")
            print(f"   Payment Date: {reg.get('payment_date')}")
    
    # Test with filters
    print("\n" + "=" * 80)
    print("TESTING WITH FILTERS")
    print("=" * 80)
    
    # Test with payment_status filter
    paid_report = service.get_registration_fees_report(payment_status='paid')
    print(f"\n✓ Paid only - Total: {paid_report['summary']['total_registrations']}")
    print(f"  Income: KSh {paid_report['summary']['total_registration_income']:,.2f}")
    
    pending_report = service.get_registration_fees_report(payment_status='pending')
    print(f"\n✓ Pending only - Total: {pending_report['summary']['total_registrations']}")
    print(f"  Outstanding: KSh {pending_report['summary']['fees_outstanding']:,.2f}")
    
    print("\n" + "=" * 80)
    print("✓ ALL TESTS COMPLETED SUCCESSFULLY!")
    print("=" * 80)

if __name__ == '__main__':
    test_registration_fees_report()
