"""
Quick fix script to add interest_schedule view and URL route
This adds the missing interest schedule functionality
"""

# Step 1: Add the URL route to loans/urls.py
urls_fix = """
# Add this line after the amortization route (around line 40):
path('<uuid:pk>/interest-schedule/', views.interest_schedule, name='interest_schedule'),
path('<uuid:pk>/receipt/', views.loan_receipt, name='loan_receipt'),
path('<uuid:pk>/amortization/', views.loan_amortization, name='loan_amortization'),
path('repayments/<uuid:pk>/edit/', views.edit_repayment, name='edit_repayment'),
path('repayments/<uuid:pk>/delete/', views.delete_repayment, name='delete_repayment'),
path('deleted/', views.deleted_loans, name='deleted_loans'),
path('<uuid:pk>/restore/', views.restore_loan, name='restore_loan'),
path('<uuid:pk>/permanently-delete/', views.permanently_delete_loan, name='permanently_delete_loan'),
path('<uuid:pk>refresh-payments/', views.refresh_loan_payments, name='refresh_loan_payments'),
path('refresh-all-payments/', views.refresh_all_payments, name='refresh_all_payments'),
"""

# Step 2: interest_schedule view code to add to loans/views.py
view_code = """
@login_required
def interest_schedule(request, pk):
    \"\"\"Display interest schedule for a loan\"\"\"
    loan = get_object_or_404(Loan, pk=pk)
    
    # Get payment schedule
    try:
        schedule = loan.get_payment_schedule()
    except:
        schedule = []
    
    #  Calculate interest breakdown
    principal = loan.principal_amount or Decimal('0.00')
    interest_amount = loan.get_display_interest_amount()
    processing_fee = loan.get_display_processing_fee_amount()
    total_amount = principal + interest_amount + processing_fee
    
    # Calculate interest paid vs outstanding
    amount_paid = loan.amount_paid or Decimal('0.00')
    if total_amount > 0:
        interest_portion = interest_amount / total_amount
        interest_paid = min(interest_amount, amount_paid * interest_portion)
        interest_outstanding = interest_amount - interest_paid
    else:
        interest_paid = Decimal('0.00')
        interest_outstanding = Decimal('0.00')
    
    context = {
        'loan': loan,
        'schedule': schedule,
        'interest_breakdown': {
            'total_interest': interest_amount,
            'interest_paid': interest_paid,
            'interest_outstanding': interest_outstanding,
            'interest_rate': loan.get_display_interest_rate(),
        }
    }
    
    return render(request, 'loans/interest_schedule.html', context)
"""

print("=" * 60)
print("INTEREST SCHEDULE ROUTE FIX")
print("=" * 60)
print("\nMISSING ROUTES TO ADD TO loans/urls.py:")
print(urls_fix)
print("\nVIEW CODE TO ADD TO loans/views.py:")
print(view_code)
print("\n" + "="*60)
print("Due to file corruption, please manually add these fixes")
print("OR the system will continue with automated fixes")
print("="*60)
