"""
Test script for new dashboard analytics
"""
import os
import django

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

from reports.simple_reports_service import simple_reports_service
from decimal import Decimal

print("=" * 80)
print("TESTING NEW DASHBOARD ANALYTICS")
print("=" * 80)

# Test completed loans analytics
print("\n1. Testing Completed Loans Analytics...")
completed_loans = simple_reports_service.get_completed_loans_analytics()
print(f"   ✓ Total Completed Loans: {completed_loans['summary']['total_completed_loans']}")
print(f"   ✓ Completed This Month: {completed_loans['summary']['completed_this_month']}")
print(f"   ✓ Completed This Year: {completed_loans['summary']['completed_this_year']}")
print(f"   ✓ Total Principal Completed: KES {completed_loans['summary']['total_principal_completed']:,.2f}")
print(f"   ✓ Total Amount Collected: KES {completed_loans['summary']['total_amount_collected']:,.2f}")

# Test overdue loans analytics
print("\n2. Testing Overdue Loans Analytics...")
overdue_loans = simple_reports_service.get_overdue_loans_analytics()
print(f"   ✓ Total Overdue Loans: {overdue_loans['summary']['total_overdue_loans']}")
print(f"   ✓ Mild Overdue (1-30 days): {overdue_loans['summary']['mild_overdue']}")
print(f"   ✓ Moderate Overdue (31-60 days): {overdue_loans['summary']['moderate_overdue']}")
print(f"   ✓ Severe Overdue (60+ days): {overdue_loans['summary']['severe_overdue']}")
print(f"   ✓ Total Overdue Amount: KES {overdue_loans['summary']['total_overdue_amount']:,.2f}")

# Test client growth analytics
print("\n3. Testing Client Growth Analytics...")
client_growth = simple_reports_service.get_client_growth_analytics()
print(f"   ✓ Total Clients: {client_growth['summary']['total_clients']}")
print(f"   ✓ Clients This Week: {client_growth['summary']['clients_this_week']}")
print(f"   ✓ Clients This Month: {client_growth['summary']['clients_this_month']}")
print(f"   ✓ Clients This Year: {client_growth['summary']['clients_this_year']}")
print(f"   ✓ Growth Rate: {client_growth['summary']['growth_rate']:.1f}%")
print(f"   ✓ Best Month: {client_growth['summary']['best_month']} ({client_growth['summary']['best_month_count']} clients)")
print(f"   ✓ Worst Month: {client_growth['summary']['worst_month']} ({client_growth['summary']['worst_month_count']} clients)")

# Test monthly breakdown
print("\n4. Monthly Client Breakdown:")
for month, count in client_growth['monthly_breakdown'].items():
    print(f"   - {month}: {count} clients")

# Test weekly breakdown (last 5 weeks)
print("\n5. Recent Weekly Client Breakdown (Last 5 Weeks):")
weekly_items = list(client_growth['weekly_breakdown'].items())[:5]
for week, count in weekly_items:
    print(f"   - {week}: {count} clients")

# Test full dashboard data generation
print("\n6. Testing Full Dashboard Data Generation...")
dashboard_data = simple_reports_service.generate_dashboard_data()
print(f"   ✓ Dashboard data generated successfully")
print(f"   ✓ Contains {len(dashboard_data)} sections")
print(f"   ✓ Sections: {', '.join(dashboard_data.keys())}")

# Verify all new sections are present
required_sections = ['completed_loans', 'overdue_loans', 'client_growth']
missing_sections = [s for s in required_sections if s not in dashboard_data]
if missing_sections:
    print(f"   ✗ Missing sections: {', '.join(missing_sections)}")
else:
    print(f"   ✓ All required sections present")

print("\n" + "=" * 80)
print("ALL TESTS COMPLETED SUCCESSFULLY!")
print("=" * 80)
print("\nNew analytics are ready to use in the Reports & Statements Dashboard.")
print("Navigate to: Reports & Statements Dashboard to see the new sections.")
