from django.core.management.base import BaseCommand
from django.db import transaction

from apps.business.marketplace.models_part1 import Vendor
from apps.business.products.models import Product


class Command(BaseCommand):
    help = "Delete all vendor profiles and vendor-owned marketplace records."

    def add_arguments(self, parser):
        parser.add_argument(
            "--confirm",
            action="store_true",
            help="Required safety flag to execute destructive deletion.",
        )

    def handle(self, *args, **options):
        if not options.get("confirm"):
            self.stdout.write(self.style.ERROR("Refusing to run without --confirm"))
            return

        vendor_count = Vendor.objects.count()
        vendor_product_count = Product.objects.filter(vendor__isnull=False).count()

        self.stdout.write(self.style.WARNING("Purging vendor data:"))
        self.stdout.write(f"  Vendors: {vendor_count}")
        self.stdout.write(f"  Products linked to vendors: {vendor_product_count}")

        with transaction.atomic():
            Vendor.objects.all().delete()

        self.stdout.write(self.style.SUCCESS("Vendor list cleaned successfully."))
