from stripe_payment.models import StripeCustomer, StripeSource
from django.core.management import BaseCommand
from progressbar import ProgressBar
from django.contrib.auth.models import User
import crayons


class Command(BaseCommand):
    help = 'Setup stripe for testing'

    def handle(self, *args, **options):
        admin = StripeCustomer.objects.filter(user__username='admin').first()

        print(crayons.red('Updating the stripe customers'))
        progress = ProgressBar(max_value=StripeCustomer.objects.count())
        count = 1

        for customer in StripeCustomer.objects.all():
            customer.stripe_customer = admin.stripe_customer
            customer.save()
            progress.update(count)
            count += 1

        print(crayons.red('Updating the stripe sources'))
        admin_source = StripeSource.objects.filter(customer__user__username='admin').first()
        progress = ProgressBar(max_value=StripeSource.objects.count())
        count = 1

        for source in StripeSource.objects.all():
            source.card = admin_source.card
            source.card_fingerprint = admin_source.card_fingerprint
            source.save()
            progress.update(count)
            count += 1

        count = 1
        print(crayons.red('Updating users'))
        progress = ProgressBar(max_value=User.objects.count())
        for user in User.objects.all():
            progress.update(count)
            if user.username == 'admin':
                continue
            if user.email == 'ernest@hookcoffee.com.sg':
                continue
            user.email = 'testing+{}@hookcoffee.com.sg'.format(count)
            user.save()
            count += 1
