from decimal import Decimal
from apps.business.wallet.models import Wallet, WalletLedger

def get_earnings_summary(user):
    """
    Aggregates earnings and balances for the user.
    Returns dict with total_earned, main_balance, repurchase_balance, locked_amount.
    """
    wallet = Wallet.objects.filter(user=user).first()
    if not wallet:
        return {
            "total_earned": Decimal('0.00'),
            "main_balance": Decimal('0.00'),
            "repurchase_balance": Decimal('0.00'),
            "locked_amount": Decimal('0.00'),
        }
    # Aggregate from ledger
    total_earned = WalletLedger.objects.filter(user=user, entry_type='CREDIT').aggregate(total=models.Sum('amount'))['total'] or Decimal('0.00')
    locked_amount = wallet.locked_amount or Decimal('0.00')
    return {
        "total_earned": total_earned,
        "main_balance": wallet.main_balance,
        "repurchase_balance": wallet.repurchase_balance,
        "locked_amount": locked_amount,
    }
