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

from accounting.models import Account, JournalEntry, JournalItem
from core.models import Expense

print("Checking Accounts...")
acc_1000 = Account.objects.filter(code='1000').first()
acc_5400 = Account.objects.filter(code='5400').first()

print(f"Account 1000 (Cash): {acc_1000}")
print(f"Account 5400 (General Expense): {acc_5400}")

print("\nChecking Journal Entries for Expenses...")
expenses = Expense.objects.all()
for exp in expenses:
    print(f"Expense {exp.id}: {exp.description} - Amount: {exp.amount}")
    # Find linked entry
    from django.contrib.contenttypes.models import ContentType
    ct = ContentType.objects.get_for_model(Expense)
    entries = JournalEntry.objects.filter(content_type=ct, object_id=exp.id)
    for entry in entries:
        print(f"  -> JournalEntry {entry.id}: {entry.description}")
        items = entry.items.all()
        if items.exists():
            for item in items:
                print(f"    -> Item: {item.account.code} {item.type} {item.amount}")
        else:
            print(f"    -> NO ITEMS FOUND!")