from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from products.models import Category, Product, ProductImage
from decimal import Decimal

class Command(BaseCommand):
    help = 'Populate database with sample Thangka products and categories'

    def handle(self, *args, **options):
        self.stdout.write('Creating sample data...')
        
        # Create categories
        categories = {
            'buddha': {
                'name': 'Buddha Thangkas',
                'description': 'Sacred paintings of various Buddha forms and manifestations'
            },
            'mandala': {
                'name': 'Mandalas',
                'description': 'Sacred geometric patterns representing the universe and enlightenment'
            },
            'tara': {
                'name': 'Tara Thangkas',
                'description': 'Compassionate female Buddha representing wisdom and liberation'
            },
            'guru': {
                'name': 'Guru Rinpoche',
                'description': 'Sacred paintings of Padmasambhava, the Lotus Born'
            },
            'dakini': {
                'name': 'Dakini',
                'description': 'Sky dancers representing wisdom and spiritual transformation'
            }
        }
        
        created_categories = {}
        for slug, data in categories.items():
            category, created = Category.objects.get_or_create(
                slug=slug,
                defaults=data
            )
            created_categories[slug] = category
            if created:
                self.stdout.write(f'Created category: {category.name}')
        
        # Create sample products
        products_data = [
            {
                'name': 'Shakyamuni Buddha Thangka',
                'category': 'buddha',
                'thangka_type': 'buddha',
                'mounting_type': 'brocade',
                'description': 'Beautiful Thangka of Shakyamuni Buddha in meditation pose, handcrafted with traditional techniques using natural pigments on cotton canvas.',
                'dimensions': '24" x 36"',
                'materials': 'Cotton canvas, natural pigments, brocade mounting',
                'artist': 'Master Thangka Artist',
                'price': Decimal('299.00'),
                'stock': 5,
                'is_featured': True
            },
            {
                'name': 'Kalachakra Mandala Thangka',
                'category': 'mandala',
                'thangka_type': 'mandala',
                'mounting_type': 'brocade',
                'description': 'Exquisite Kalachakra Mandala representing the wheel of time, featuring intricate geometric patterns and sacred symbols.',
                'dimensions': '30" x 30"',
                'materials': 'Cotton canvas, gold leaf, natural pigments, brocade mounting',
                'artist': 'Senior Mandala Artist',
                'price': Decimal('450.00'),
                'sale_price': Decimal('399.00'),
                'stock': 3,
                'is_featured': True
            },
            {
                'name': 'Green Tara Thangka',
                'category': 'tara',
                'thangka_type': 'tara',
                'mounting_type': 'brocade',
                'description': 'Compassionate Green Tara in her traditional pose, offering protection and swift assistance to all beings.',
                'dimensions': '20" x 28"',
                'materials': 'Cotton canvas, natural pigments, brocade mounting',
                'artist': 'Tara Specialist',
                'price': Decimal('350.00'),
                'stock': 4,
                'is_featured': True
            },
            {
                'name': 'Guru Rinpoche Thangka',
                'category': 'guru',
                'thangka_type': 'guru',
                'mounting_type': 'brocade',
                'description': 'Sacred image of Guru Rinpoche (Padmasambhava) in his peaceful form, blessing all who view it.',
                'dimensions': '22" x 32"',
                'materials': 'Cotton canvas, natural pigments, brocade mounting',
                'artist': 'Guru Rinpoche Master',
                'price': Decimal('380.00'),
                'stock': 2,
                'is_featured': True
            },
            {
                'name': 'Medicine Buddha Thangka',
                'category': 'buddha',
                'thangka_type': 'buddha',
                'mounting_type': 'brocade',
                'description': 'Healing Medicine Buddha in deep blue color, radiating healing energy and compassion.',
                'dimensions': '26" x 34"',
                'materials': 'Cotton canvas, natural pigments, brocade mounting',
                'artist': 'Healing Arts Specialist',
                'price': Decimal('320.00'),
                'stock': 6,
                'is_featured': False
            },
            {
                'name': 'White Tara Thangka',
                'category': 'tara',
                'thangka_type': 'tara',
                'mounting_type': 'brocade',
                'description': 'Pure White Tara representing longevity and wisdom, painted with meticulous attention to detail.',
                'dimensions': '24" x 30"',
                'materials': 'Cotton canvas, natural pigments, brocade mounting',
                'artist': 'Tara Specialist',
                'price': Decimal('360.00'),
                'stock': 3,
                'is_featured': False
            },
            {
                'name': 'Vajrayogini Dakini Thangka',
                'category': 'dakini',
                'thangka_type': 'dakini',
                'mounting_type': 'brocade',
                'description': 'Powerful Vajrayogini in her traditional form, representing the highest wisdom and transformation.',
                'dimensions': '28" x 36"',
                'materials': 'Cotton canvas, natural pigments, brocade mounting',
                'artist': 'Dakini Master',
                'price': Decimal('420.00'),
                'stock': 2,
                'is_featured': False
            },
            {
                'name': 'Amitabha Buddha Thangka',
                'category': 'buddha',
                'thangka_type': 'buddha',
                'mounting_type': 'brocade',
                'description': 'Amitabha Buddha of Infinite Light in his Western Pure Land, radiating boundless compassion.',
                'dimensions': '25" x 33"',
                'materials': 'Cotton canvas, natural pigments, brocade mounting',
                'artist': 'Pure Land Specialist',
                'price': Decimal('340.00'),
                'stock': 4,
                'is_featured': False
            }
        ]
        
        for product_data in products_data:
            category = created_categories[product_data['category']]
            
            # Remove category from product_data before creating Product
            product_data.pop('category')
            
            product, created = Product.objects.get_or_create(
                name=product_data['name'],
                defaults={
                    'category': category,
                    'slug': product_data['name'].lower().replace(' ', '-').replace('(', '').replace(')', ''),
                    **product_data
                }
            )
            
            if created:
                self.stdout.write(f'Created product: {product.name}')
        
        self.stdout.write(self.style.SUCCESS('Sample data created successfully!'))
        self.stdout.write(f'Created {len(created_categories)} categories and {len(products_data)} products')
