"""
Fix all amount_paid field references in users/views.py
Replace with proper Repayment calculations
"""
import re

# Read the file
with open('users/views.py', 'r', encoding='utf-8') as f:
    content = f.read()

# Pattern 1: Sum('amount_paid') in aggregates
pattern1 = r"\.aggregate\([^)]*Sum\('amount_paid'\)[^)]*\)\['total'\]"
pattern2 = r"Sum\('amount_paid'\)"
pattern3 = r"Sum\('loan__amount_paid'\)"
pattern4 = r"Sum\('loans__amount_paid'[^)]*\)"

# Count occurrences
count1 = len(re.findall(pattern1, content))
count2 = len(re.findall(pattern2, content))
count3 = len(re.findall(pattern3, content))
count4 = len(re.findall(pattern4, content))

print(f"Found {count1} aggregate patterns")
print(f"Found {count2} Sum('amount_paid') patterns")
print(f"Found {count3} Sum('loan__amount_paid') patterns")
print(f"Found {count4} Sum('loans__amount_paid') patterns")

# Show the lines
lines = content.split('\n')
for i, line in enumerate(lines, 1):
    if 'amount_paid' in line and ('Sum(' in line or 'aggregate' in line):
        print(f"\nLine {i}: {line.strip()}")
