from django.core.management.base import BaseCommand
from django.contrib.contenttypes.models import ContentType
from django.db import transaction
from hookmigration.models import Transient
from hookmigration.peewee_models import CustomersCustomer
from django.contrib.auth.models import User
from oscar.core.loading import get_model
import crayons
from subscription.models import Subscription
from progressbar import ProgressBar
from referral.models import Referrer, UserReferrer
from loyalty.models import Point
from stripe_payment.models import StripeTransaction

UserAddress = get_model('address', 'UserAddress')
Country = get_model('address', 'Country')
Product = get_model('catalogue', 'Product')
ShippingAddress = get_model('order', 'ShippingAddress')
Order = get_model('order', 'Order')


class Command(BaseCommand):
    help = 'Migrate all the blog articles'

    def delete(self, qs, name):
        total = qs.count()
        progress = ProgressBar(max_value=total)
        deleted = 0
        print('Deleting {} {}'.format(crayons.red(total), crayons.blue(name)))
        for item in qs.all():
            item.delete()
            deleted += 1
            progress.update(deleted)

    def handle(self, *args, **options):
        # Delete all the referrers
        self.delete(Referrer.objects.all(), 'Referrals')
        # Delete all the subscriptions
        self.delete(Subscription.objects.all(), 'Subscriptions')
        # Delete users
        self.delete(User.objects.exclude(username='admin'), 'Users')
        # delete all products with pods and coffee products class
        self.delete(Product.objects.filter(product_class__slug__in=['coffee-bag', 'coffee-pods']), 'Products')
        # delete all transients
        self.delete(Transient.objects.all(), 'Trannies')
        # Delete all the orders
        self.delete(Order.objects.all(), 'Orders')
        # Delete all the shipping addresses
        self.delete(ShippingAddress.objects.all(), 'Shipping Addresses')
        # Delete the loyalty points
        self.delete(Point.objects.all(), 'Loyalty points')
        # Stripe transactions
        self.delete(StripeTransaction.objects.all(), 'Stripe transactions')
