"""
Django management command to populate categories with sample data.
Run with: python manage.py populate_categories
"""

from django.core.management.base import BaseCommand

# Import your models - adjust the import path based on your app structure
from apps.inventory.models import Category


class Command(BaseCommand):
    help = 'Populates categories table with sample data'

    def add_arguments(self, parser):
        parser.add_argument(
            '--clear',
            action='store_true',
            help='Clear existing categories before populating',
        )

    def handle(self, *args, **options):
        if options['clear']:
            self.stdout.write('Clearing existing categories...')
            self.clear_data()

        self.stdout.write('Starting category population...')

        categories = self.create_categories()

        self.stdout.write(self.style.SUCCESS('Successfully populated categories!'))
        self.print_summary(categories)

    def clear_data(self):
        """Clear existing categories"""
        Category.objects.all().delete()
        self.stdout.write(self.style.WARNING('Cleared existing categories'))

    def create_categories(self):
        """Create product categories"""
        categories_data = [
            {
                'name': 'Laptops',
                'description': 'Portable computers including notebooks and ultrabooks'
            },
            {
                'name': 'Desktops',
                'description': 'Desktop computers and workstations'
            },
            {
                'name': 'Monitors',
                'description': 'Display screens and monitors'
            },
            {
                'name': 'Peripherals',
                'description': 'Computer accessories like keyboards, mice, and webcams'
            },
            {
                'name': 'Components',
                'description': 'Computer parts including RAM, storage, and graphics cards'
            },
            {
                'name': 'Networking',
                'description': 'Network equipment including routers, switches, and cables'
            },
            {
                'name': 'Printers',
                'description': 'Printing devices and scanners'
            },
            {
                'name': 'Storage',
                'description': 'External storage devices and NAS systems'
            },
            {
                'name': 'Audio',
                'description': 'Speakers, headphones, and audio equipment'
            },
            {
                'name': 'Cables & Adapters',
                'description': 'Various cables, adapters, and connectors'
            },
        ]
        
        categories = []
        for data in categories_data:
            category, created = Category.objects.get_or_create(
                name=data['name'],
                defaults={'description': data['description']}
            )
            if created:
                self.stdout.write(
                    self.style.SUCCESS(f'  ✓ Created category: {category.name}')
                )
            else:
                self.stdout.write(
                    self.style.WARNING(f'  • Category already exists: {category.name}')
                )
            categories.append(category)
        
        return categories

    def print_summary(self, categories):
        """Print summary of created data"""
        self.stdout.write('\n' + '='*50)
        self.stdout.write(self.style.SUCCESS('CATEGORY POPULATION SUMMARY'))
        self.stdout.write('='*50)
        self.stdout.write(f'Total Categories: {len(categories)}')
        self.stdout.write('='*50 + '\n')