from django.core.management.base import BaseCommand
from appauthentication.models import GivingCircle


class Command(BaseCommand):
    help = 'Populate initial Giving Circles data'

    def handle(self, *args, **options):
        giving_circles = [
            {'name': 'Teranga', 'monthly_amount': 10.00, 'order': 1, 'description': 'Cercle d\'entrée pour les petits donateurs réguliers'},
            {'name': 'Soleil', 'monthly_amount': 40.00, 'order': 2, 'description': 'Cercle intermédiaire pour les donateurs engagés'},
            {'name': 'Royaume', 'monthly_amount': 160.00, 'order': 3, 'description': 'Cercle avancé pour les grands donateurs'},
            {'name': 'Héritage', 'monthly_amount': 1000.00, 'order': 4, 'description': 'Cercle prestigieux pour les donateurs majeurs'},
        ]

        created_count = 0
        updated_count = 0

        for circle_data in giving_circles:
            circle, created = GivingCircle.objects.get_or_create(
                name=circle_data['name'],
                defaults={
                    'monthly_amount': circle_data['monthly_amount'],
                    'order': circle_data['order'],
                    'description': circle_data['description']
                }
            )
            
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'✓ Created: {circle.name} - {circle.monthly_amount}€/mois')
                )
            else:
                # Update existing circle if amounts differ
                if circle.monthly_amount != circle_data['monthly_amount'] or circle.order != circle_data['order']:
                    circle.monthly_amount = circle_data['monthly_amount']
                    circle.order = circle_data['order']
                    circle.description = circle_data['description']
                    circle.save()
                    updated_count += 1
                    self.stdout.write(
                        self.style.WARNING(f'⟳ Updated: {circle.name} - {circle.monthly_amount}€/mois')
                    )
                else:
                    self.stdout.write(
                        self.style.NOTICE(f'→ Already exists: {circle.name} - {circle.monthly_amount}€/mois')
                    )

        self.stdout.write(
            self.style.SUCCESS(f'\n✓ Summary: {created_count} created, {updated_count} updated')
        )
