#!/bin/bash
# Delete incorrect rewards for investment #17 (created Dec 20, should have no rewards yet)

cd "$(dirname "$0")/.."

# Source environment and run Django command
set -a
source .env
set +a

python3 manage.py shell -c "
from apps.investments.models import Investment, Reward
from datetime import date

# Investment #17 was created on Dec 20
# It should have no rewards until we run the script on Dec 22 (for Dec 21)
inv = Investment.objects.get(id=17)
print(f'Investment #{inv.id}: created {inv.created_at}, start {inv.start_date}')

# Delete any rewards with reward_date <= start_date (these are incorrect)
incorrect_rewards = Reward.objects.filter(
    investment=inv,
    reward_date__lte=inv.start_date.date()
)

count = incorrect_rewards.count()
print(f'Found {count} incorrect rewards (reward_date <= start_date)')

if count > 0:
    for r in incorrect_rewards:
        print(f'  Deleting: reward_date={r.reward_date}, amount={r.amount}')
    incorrect_rewards.delete()
    print(f'Deleted {count} incorrect rewards')
else:
    print('No incorrect rewards found')
"
