#!/usr/bin/env python3
"""
Test script for comprehensive analytics dashboard with product breakdowns
"""
import os
import sys
import django

# Setup Django environment
sys.path.insert(0, os.path.dirname(__file__))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from reports.comprehensive_reports import reports_service
from loans.models import Loan, LoanProduct
from users.models import CustomUser
from django.utils import timezone
from decimal import Decimal

def test_comprehensive_analytics():
    """Test the comprehensive analytics functionality"""
    print("🧪 Testing Comprehensive Analytics Dashboard...")
    print("=" * 60)
    
    try:
        # Test 1: Generate comprehensive dashboard data
        print("\n1. Testing comprehensive dashboard data generation...")
        dashboard_data = reports_service.generate_comprehensive_dashboard_data()
        
        if dashboard_data and 'summary_metrics' in dashboard_data:
            print("✅ Dashboard data generated successfully")
            
            # Display enhanced summary metrics
            metrics = dashboard_data['summary_metrics']
            print(f"\n📊 Enhanced Portfolio Overview:")
            print(f"   Total Loans: {metrics.get('total_active_loans', 0)}")
            print(f"   Average Amount: KES {metrics.get('avg_loan_amount', 0):,.2f}")
            print(f"   Active Loans: {metrics.get('total_active_loans', 0)}")
            print(f"   Overdue: {metrics.get('overdue_loans', 0)}")
            print(f"   Total Disbursed: KES {metrics.get('total_disbursed', 0):,.2f}")
            print(f"   Default Rate: {metrics.get('default_rate', 0):.1f}%")
            print(f"   Total Collected: KES {metrics.get('total_collected', 0):,.2f}")
            print(f"   Collection Rate: {metrics.get('collection_rate', 0):.1f}%")
            
            # Display product breakdown
            if metrics.get('product_breakdown'):
                print(f"\n📈 Product Breakdown:")
                for product in metrics['product_breakdown']:
                    product_type = product.get('application__loan_product__product_type', 'Unknown')
                    count = product.get('count', 0)
                    total_amount = product.get('total_amount', 0)
                    avg_amount = product.get('avg_amount', 0)
                    print(f"   {product_type.title()}: {count} loans, KES {total_amount:,.2f} total, KES {avg_amount:,.2f} avg")
            
            # Display collection rates by product
            if metrics.get('collection_by_product'):
                print(f"\n💰 Collection Rates by Product:")
                for product_type, data in metrics['collection_by_product'].items():
                    rate = data.get('collection_rate', 0)
                    collected = data.get('collected_amount', 0)
                    disbursed = data.get('disbursed_amount', 0)
                    print(f"   {product_type.title()}: {rate:.1f}% (KES {collected:,.2f} / KES {disbursed:,.2f})")
            
            # Display outstanding by product
            if metrics.get('outstanding_by_product'):
                print(f"\n⚠️  Outstanding Loans by Product:")
                for product in metrics['outstanding_by_product']:
                    product_type = product.get('product_type', 'Unknown')
                    count = product.get('count', 0)
                    outstanding = product.get('outstanding_amount', 0)
                    print(f"   {product_type.title()}: {count} loans, KES {outstanding:,.2f} outstanding")
        else:
            print("❌ Failed to generate dashboard data")
            return False
        
        # Test 2: Test comprehensive product analytics
        print("\n2. Testing comprehensive product analytics...")
        if 'comprehensive_analytics' in dashboard_data:
            analytics = dashboard_data['comprehensive_analytics']
            
            if analytics.get('product_analytics'):
                print("✅ Product analytics generated successfully")
                
                for product_type, data in analytics['product_analytics'].items():
                    print(f"\n📊 {product_type.title()} Analytics:")
                    
                    # Active loans metrics
                    active_loans = data.get('active_loans', {})
                    print(f"   Active Loans: {active_loans.get('count', 0)}")
                    print(f"   Total Amount: KES {active_loans.get('total_amount', 0):,.2f}")
                    print(f"   Average Amount: KES {active_loans.get('avg_amount', 0):,.2f}")
                    print(f"   Outstanding: KES {active_loans.get('outstanding_amount', 0):,.2f}")
                    
                    # Collection metrics
                    collection = data.get('collection_metrics', {})
                    print(f"   Collection Rate: {collection.get('collection_rate', 0):.1f}%")
                    print(f"   Collected: KES {collection.get('collected_amount', 0):,.2f}")
                    
                    # Risk metrics
                    risk = data.get('risk_metrics', {})
                    print(f"   Due Today: {risk.get('due_today', 0)}")
                    print(f"   Overdue: {risk.get('overdue', 0)}")
                    print(f"   Default Rate: {risk.get('default_rate', 0):.1f}%")
                    
                    # Revenue metrics
                    revenue = data.get('revenue_metrics', {})
                    print(f"   Monthly Processing Fees: KES {revenue.get('monthly_processing_fees', 0):,.2f}")
                    print(f"   Monthly Interest: KES {revenue.get('monthly_interest', 0):,.2f}")
                    print(f"   Total Monthly Revenue: KES {revenue.get('total_monthly_revenue', 0):,.2f}")
            
            # Display summary insights
            summary = analytics.get('summary', {})
            if summary:
                print(f"\n🎯 Analytics Summary:")
                print(f"   Total Products: {summary.get('total_products', 0)}")
                if summary.get('best_performing_product'):
                    print(f"   Best Performing: {summary['best_performing_product'].title()}")
                if summary.get('highest_volume_product'):
                    print(f"   Highest Volume: {summary['highest_volume_product'].title()}")
                if summary.get('most_loans_product'):
                    print(f"   Most Loans: {summary['most_loans_product'].title()}")
        
        # Test 3: Test processing fees with product breakdown
        print("\n3. Testing processing fees with product breakdown...")
        processing_fees = dashboard_data.get('processing_fees_current_month', {})
        if processing_fees.get('product_breakdown'):
            print("✅ Processing fees product breakdown available")
            
            for product in processing_fees['product_breakdown']:
                product_type = product.get('application__loan_product__product_type', 'Unknown')
                total_fees = product.get('total_fees', 0)
                loan_count = product.get('loan_count', 0)
                percentage = product.get('percentage_of_total_fees', 0)
                print(f"   {product_type.title()}: KES {total_fees:,.2f} ({percentage:.1f}%) from {loan_count} loans")
        
        # Test 4: Test interest income with product breakdown
        print("\n4. Testing interest income with product breakdown...")
        interest_income = dashboard_data.get('interest_income_current_month', {})
        if interest_income.get('product_breakdown'):
            print("✅ Interest income product breakdown available")
            
            for product in interest_income['product_breakdown']:
                product_type = product.get('application__loan_product__product_type', 'Unknown')
                total_interest = product.get('total_interest', 0)
                loan_count = product.get('loan_count', 0)
                percentage = product.get('percentage_of_total_interest', 0)
                print(f"   {product_type.title()}: KES {total_interest:,.2f} ({percentage:.1f}%) from {loan_count} loans")
        
        print("\n✅ All comprehensive analytics tests passed!")
        return True
        
    except Exception as e:
        print(f"❌ Error during testing: {e}")
        import traceback
        traceback.print_exc()
        return False

def display_sample_data():
    """Display some sample data for context"""
    print("\n📋 Sample Data Context:")
    
    # Count loans by product type
    try:
        active_loans = Loan.objects.filter(status='active')
        total_loans = active_loans.count()
        
        if total_loans > 0:
            print(f"   Total Active Loans: {total_loans}")
            
            # Group by product type
            product_counts = {}
            for loan in active_loans.select_related('application__loan_product'):
                if loan.application and loan.application.loan_product:
                    product_type = loan.application.loan_product.product_type
                    product_counts[product_type] = product_counts.get(product_type, 0) + 1
            
            for product_type, count in product_counts.items():
                percentage = (count / total_loans) * 100
                print(f"   {product_type.title()}: {count} loans ({percentage:.1f}%)")
        else:
            print("   No active loans found")
            
        # Count loan products
        products = LoanProduct.objects.filter(is_active=True)
        print(f"   Active Loan Products: {products.count()}")
        for product in products:
            print(f"     - {product.name} ({product.product_type})")
            
    except Exception as e:
        print(f"   Error getting sample data: {e}")

if __name__ == "__main__":
    display_sample_data()
    success = test_comprehensive_analytics()
    
    if success:
        print("\n🚀 Comprehensive Analytics Dashboard is ready!")
        print("\n📋 Next Steps:")
        print("   1. Visit /reports/comprehensive/ to see the enhanced dashboard")
        print("   2. All analytics include detailed product breakdowns")
        print("   3. Collection rates are calculated per product")
        print("   4. Outstanding loans are categorized by product")
        print("   5. Revenue metrics show processing fees and interest by product")
    else:
        print("\n❌ Some tests failed. Please check the errors above.")