import os
import django

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

from expenses.models import Expense
from django.db.models import Sum

print("=" * 60)
print("EXPENSES SYSTEM VERIFICATION")
print("=" * 60)

total = Expense.objects.count()
approved = Expense.objects.filter(status='approved').count()
pending = Expense.objects.filter(status='pending').count()
rejected = Expense.objects.filter(status='rejected').count()

total_amount = Expense.objects.filter(status='approved').aggregate(
    total=Sum('amount')
)['total'] or 0

print(f"\n📊 Statistics:")
print(f"  Total Expenses: {total}")
print(f"  ✅ Approved: {approved}")
print(f"  ⏳ Pending: {pending}")
print(f"  ❌ Rejected: {rejected}")
print(f"  💰 Total Approved Amount: KES {total_amount:,.2f}")

print(f"\n📁 Categories:")
categories = Expense.objects.values_list('category', flat=True).distinct()
for cat in categories:
    count = Expense.objects.filter(category=cat).count()
    print(f"  - {cat.title()}: {count}")

print(f"\n💳 Payment Methods:")
methods = Expense.objects.values_list('payment_method', flat=True).distinct()
for method in methods:
    count = Expense.objects.filter(payment_method=method).count()
    print(f"  - {method.title()}: {count}")

print(f"\n🏢 Branches:")
from users.models import Branch
branches = Branch.objects.all()
for branch in branches:
    count = Expense.objects.filter(branch=branch).count()
    if count > 0:
        print(f"  - {branch.name}: {count} expenses")

print("\n" + "=" * 60)
print("✅ VERIFICATION COMPLETE - System is working!")
print("=" * 60)
