import os
import django
import random
from datetime import date, timedelta

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

from core.models import Voter, Donation, EventParticipation

def populate_engagement_data():
    voters = Voter.objects.all()
    
    event_types = ['Town Hall', 'Neighborhood Walk', 'Phone Bank', 'Fundraiser Dinner', 'Rally']
    event_descriptions = [
        'Discussed local infrastructure issues.',
        'Canvassed the precinct with volunteers.',
        'Made 50 calls to likely voters.',
        'Annual gala for the candidate.',
        'Major campaign kickoff event.'
    ]
    
    for voter in voters:
        # 30% chance of having donations
        if random.random() < 0.3:
            num_donations = random.randint(1, 4)
            for _ in range(num_donations):
                Donation.objects.create(
                    voter=voter,
                    donation_date=date.today() - timedelta(days=random.randint(1, 365)),
                    amount=random.choice([10, 25, 50, 100, 250, 500])
                )
        
        # 40% chance of having events
        if random.random() < 0.4:
            num_events = random.randint(1, 3)
            for _ in range(num_events):
                EventParticipation.objects.create(
                    voter=voter,
                    event_date=date.today() - timedelta(days=random.randint(1, 180)),
                    event_type=random.choice(event_types),
                    description=random.choice(event_descriptions)
                )

    print(f"Populated donations and events for {voters.count()} voters.")

if __name__ == '__main__':
    populate_engagement_data()
