"""
Anonymize all userdata present in the database
"""

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand

from oscar.core.loading import get_model

User = get_user_model()

UserAddress = get_model("address", "UserAddress")
CommunicationEvent = get_model("order", "CommunicationEvent")
ShippingAddress = get_model("order", "ShippingAddress")
BillingAddress = get_model("order", "BillingAddress")
ProductReview = get_model("reviews", "ProductReview")

try:
    Subscription = get_model("newsletter", "Subscription")
except LookupError:
    Subscription = ShippingAddress


class Command(BaseCommand):
    help = __doc__

    def add_arguments(self, parser):
        parser.add_argument(
            "--no-dry-run", action="store_true", help="Really commit to the database"
        )

    def handle(self, *args, **options):
        User.objects.all().update(email="", first_name="", last_name="")
        ProductReview.objects.all().update(email="", name="Anonymous")
        UserAddress.objects.all().delete()
        CommunicationEvent.objects.all().delete()
        ShippingAddress.objects.all().delete()
        BillingAddress.objects.all().delete()
        Subscription.objects.all().delete()
