from django.core.management.base import BaseCommand
from shop.models import Color, Size, Product
from django.conf import settings

class Command(BaseCommand):
    help = 'Creates initial data for the shop'

    def handle(self, *args, **kwargs):
        # Create colors
        self.stdout.write('Creating colors...')
        colors = [
            {'name': 'Blanc', 'hex': '#FFFFFF', 'secondary_hex': None, 'ordering': 10},
            {'name': 'Blanc / Bleu Sport', 'hex': '#FFFFFF', 'secondary_hex': '#112B44', 'ordering': 11},
            {'name': 'Blanc / Gris Clair', 'hex': '#FFFFFF', 'secondary_hex': '#D3D3D3', 'ordering': 12},
            {'name': 'Bleu Sport', 'hex': '#112B44', 'secondary_hex': None, 'ordering': 20},
            {'name': 'Bleu Sport / Blanc', 'hex': '#112B44', 'secondary_hex': '#FFFFFF', 'ordering': 21},
            {'name': 'Bleu Sport / Bleu Sport Chiné', 'hex': '#112B44', 'secondary_hex': '#16395A', 'ordering': 22},
            {'name': 'Fuchsia', 'hex': '#C1366B', 'secondary_hex': None, 'ordering': 30},
            {'name': 'Corail / Noir', 'hex': '#FF7F50', 'secondary_hex': '#000000', 'ordering': 40},
            {'name': 'Gris Foncé Chiné / Noir', 'hex': '#4D4D4D', 'secondary_hex': '#000000', 'ordering': 50},
            {'name': 'Noir', 'hex': '#333333', 'secondary_hex': None, 'ordering': 60},
            {'name': 'Noir / Corail', 'hex': '#000000', 'secondary_hex': '#FF7F50', 'ordering': 61},
            {'name': 'Noir / Gris Foncé Chiné', 'hex': '#000000', 'secondary_hex': '#4D4D4D', 'ordering': 62},
        ]

        color_objects = {}
        for color_data in colors:
            color, created = Color.objects.get_or_create(
                name=color_data['name'],
                defaults={
                    'colorHex': color_data['hex'],
                    'secondary_hex_color': color_data['secondary_hex'],
                    'ordering': color_data['ordering']
                }
            )
            color_objects[color_data['name']] = color
            if created:
                self.stdout.write(f'Created color: {color_data["name"]}')
            else:
                color.colorHex = color_data['hex']
                color.secondary_hex_color = color_data['secondary_hex']
                color.ordering = color_data['ordering']
                color.save()
                self.stdout.write(f'Updated color: {color_data["name"]}')

        # Create sizes
        self.stdout.write('Creating sizes...')
        sizes = ['Taille Unique', 'XS', 'S', 'M', 'L', 'XL', 'XXL', '3XL', '4XL']

        size_objects = {}
        for name in sizes:
            size, created = Size.objects.get_or_create(name=name)
            size_objects[name] = size
            if created:
                self.stdout.write(f'Created size: {name}')
            else:
                self.stdout.write(f'Size already exists: {name}')

        # Create products
        self.stdout.write('Creating products...')
        products = [
            {
                'sku': 'PC001',
                'title': 'Padel Club Cap',
                'description': 'Casquette logo centre',
                'price': 25.00,
                'ordering_value': 1,
                'cut': 0,  # Unisex
                'colors': ['Blanc', 'Bleu Sport', 'Noir'],
                'sizes': ['Taille Unique'],
                'image_filename': 'hat.jpg'
            },
            {
               'sku': 'PC002',
               'title': 'Padel Club Hoodie Femme',
               'description': 'Hoodie femme logo cœur et dos',
               'price': 50.00,
               'ordering_value': 10,
               'cut': 1,
               'colors': ['Blanc', 'Bleu Sport', 'Noir', 'Fuchsia'],
               'sizes': ['XS', 'S', 'M', 'L', 'XL', 'XXL'],
               'image_filename': 'PS_K473_WHITE.png.avif'
            },
            {
               'sku': 'PC003',
               'title': 'Padel Club Hoodie Homme',
               'description': 'Hoodie homme logo cœur et dos',
               'price': 50.00,
               'ordering_value': 11,
               'cut': 2,
               'colors': ['Blanc', 'Bleu Sport', 'Noir', 'Fuchsia'],
               'sizes': ['XS', 'S', 'M', 'L', 'XL', 'XXL', '3XL', '4XL'],
               'image_filename': 'PS_K476_WHITE.png.avif'
            },
            {
                'sku': 'PC004',
                'title': 'Débardeur Femme',
                'description': 'Débardeur femme avec logo coeur.',
                'price': 25.00,
                'ordering_value': 20,
                'cut': 1,  # Women
                'colors': ['Blanc / Bleu Sport', 'Noir / Corail', 'Noir / Gris Foncé Chiné'],
                'sizes': ['XS', 'S', 'M', 'L', 'XL'],
                'image_filename': 'PS_PA4031_WHITE-SPORTYNAVY.png.avif'
            },
            {
               'sku': 'PC005',
               'title': 'Jupe bicolore Femme',
               'description': 'Avec short intégré logo jambe (sauf corail)',
               'price': 30.00,
               'ordering_value': 30,
               'cut': 1,  # Women
               'colors': ['Blanc / Bleu Sport', 'Bleu Sport / Blanc', 'Corail / Noir', 'Noir / Gris Foncé Chiné'],
               'sizes': ['XS', 'S', 'M', 'L', 'XL'],
               'image_filename': 'PS_PA1031_WHITE-SPORTYNAVY.png.avif'
            },
            {
                'sku': 'PC006',
                'title': 'T-shirt Bicolore Homme',
                'description': 'T-shirt bicolore avec logo coeur.',
                'price': 25.00,
                'ordering_value': 40,
                'cut': 2,  # Men
                'colors': ['Blanc / Gris Clair', 'Bleu Sport / Blanc', 'Bleu Sport / Bleu Sport Chine', 'Noir', 'Noir / Gris Foncé Chiné'],
                'sizes': ['S', 'M', 'L', 'XL', 'XXL', '3XL'],
                'image_filename': 'tshirt_h.png'
            },
            {
                'sku': 'PC007',
                'title': 'Short Bicolore Homme',
                'description': 'Short bicolore avec logo jambe.',
                'price': 30.00,
                'ordering_value': 50,
                'cut': 2,  # Men
                'colors': ['Blanc / Bleu Sport', 'Blanc / Gris Clair', 'Noir', 'Gris Foncé Chiné / Noir'],
                'sizes': ['S', 'M', 'L', 'XL', 'XXL', '3XL'],
                'image_filename': 'PS_PA1030_WHITE-SPORTYNAVY.png.avif'
            },
        ]

        for product_data in products:
            product, created = Product.objects.update_or_create(
                sku=product_data['sku'],
                defaults={
                    'title': product_data['title'],
                    'description': product_data.get('description', ''),
                    'price': product_data['price'],
                    'ordering_value': product_data['ordering_value'],
                    'cut': product_data['cut']
                }
            )

            if created:
                self.stdout.write(f'Created product: {product_data["sku"]} - {product_data["title"]}')
            else:
                self.stdout.write(f'Updated product: {product_data["sku"]} - {product_data["title"]}')

            # Handle the image path
            if 'image_filename' in product_data and product_data['image_filename']:
                image_path = f"{settings.STATIC_URL}shop/images/products/{product_data['image_filename']}"
                if product.image != image_path:
                    product.image = image_path
                    product.save()
                    self.stdout.write(f'Updated image path to "{image_path}" for: {product_data["sku"]}')

            # Update colors - first clear existing then add new ones
            product.colors.clear()
            for color_name in product_data['colors']:
                if color_name in color_objects:
                    product.colors.add(color_objects[color_name])
            self.stdout.write(f'Updated colors for: {product_data["sku"]}')

            # Update sizes - first clear existing then add new ones
            product.sizes.clear()
            for size_name in product_data['sizes']:
                if size_name in size_objects:
                    product.sizes.add(size_objects[size_name])
            self.stdout.write(f'Updated sizes for: {product_data["sku"]}')

        self.stdout.write(self.style.SUCCESS('Successfully created/updated shop data'))
