"""
Management command to seed sub-categories under the existing top-level
categories. Idempotent (get_or_create per name+category) — safe to re-run.

Usage:
    python manage.py seed_subcategories
"""
from django.core.management.base import BaseCommand
from business.models import Category, SubCategory

SUBCATEGORIES = {
    "Art and Entertainment": ["Live Music Venues", "Art Galleries", "Event Planning", "Photography Studios"],
    "Automotive": ["Auto Repair & Servicing", "Car Wash & Detailing", "Auto Parts", "Car Dealerships"],
    "Beauty and Wellness": ["Hair Salons", "Nail Studios", "Spas & Massage", "Barbershops"],
    "Education and Training": ["Tutoring Centers", "Language Schools", "Vocational Training", "Driving Schools"],
    "Engineering and Construction": ["General Contractors", "Electricians", "Plumbers", "Architects"],
    "Food and Restaurants": ["Nigerian Cuisine", "Fast Food", "Cafes & Bakeries", "Catering Services"],
    "Health and Medical": ["Clinics", "Pharmacies", "Dentists", "Diagnostic Labs"],
    "Home and Services": ["Cleaning Services", "Interior Design", "Pest Control", "Appliance Repair"],
    "Lawyers": ["Corporate Law", "Family Law", "Real Estate Law", "Criminal Defense"],
    "Shopping and Retail": ["Clothing & Fashion", "Electronics Stores", "Supermarkets", "Gift Shops"],
    "Technology": ["IT Services", "Web & App Development", "Phone & Computer Repair", "Software Solutions"],
    # "Restaurant" (distinct from "Food and Restaurants", 0 businesses) is
    # deliberately left out — looks like leftover data from before the
    # category taxonomy settled, not something to build sub-categories on.
}


class Command(BaseCommand):
    help = "Seed sub-categories under existing top-level categories"

    def handle(self, *args, **options):
        created_count = 0
        skipped_categories = []

        for category_name, sub_names in SUBCATEGORIES.items():
            try:
                category = Category.objects.get(name=category_name)
            except Category.DoesNotExist:
                skipped_categories.append(category_name)
                continue

            for sub_name in sub_names:
                _, created = SubCategory.objects.get_or_create(
                    category=category,
                    name=sub_name,
                )
                if created:
                    created_count += 1
                    self.stdout.write(f"  Created: {category_name} -> {sub_name}")

        if skipped_categories:
            self.stdout.write(self.style.WARNING(f"Categories not found, skipped: {skipped_categories}"))

        self.stdout.write(self.style.SUCCESS(f"Done. Created {created_count} new sub-categories."))
