#!/usr/bin/env python
"""
Test the processing fees report fix
"""
import os
import sys
import django

# Setup Django
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from reports.simple_reports_service import SimpleReportsService
from django.utils import timezone

print("=" * 80)
print("TESTING PROCESSING FEES REPORT FIX")
print("=" * 80)

# Create service instance
service = SimpleReportsService()

# Test with current month
print("\n1. Testing with current month...")
report = service.get_processing_fees_report(period='month')

print(f"\nSummary:")
print(f"  Total Processing Fees: KSh {report['summary']['total_processing_fees']:,.2f}")
print(f"  Total Loans Processed: {report['summary']['total_loans_processed']}")
print(f"  Average Fee: KSh {report['summary']['average_fee']:,.2f}")

print(f"\nLoans with Processing Fees:")
for loan in report['loans']:
    print(f"  - {loan['loan_number']}: {loan['borrower_name']}")
    print(f"    Principal: KSh {loan['loan_amount']:,.2f}")
    print(f"    Processing Fee: KSh {loan['processing_fee']:,.2f} ({loan['fee_percentage']:.2f}%)")
    print(f"    Date: {loan['created_date']}")

print(f"\nMonthly Breakdown:")
for month, data in report['monthly_breakdown'].items():
    print(f"  {month}: {data['count']} loans, KSh {data['amount']:,.2f}")

# Test with all time
print("\n" + "=" * 80)
print("2. Testing with all time (90 days)...")
report_all = service.get_processing_fees_report(period='quarter')

print(f"\nSummary:")
print(f"  Total Processing Fees: KSh {report_all['summary']['total_processing_fees']:,.2f}")
print(f"  Total Loans Processed: {report_all['summary']['total_loans_processed']}")
print(f"  Average Fee: KSh {report_all['summary']['average_fee']:,.2f}")

print("\n" + "=" * 80)
print("TEST COMPLETE - Fix is working!" if report['summary']['total_processing_fees'] > 0 else "WARNING: No fees found!")
print("=" * 80)
