"""
Initialize Credit Cost Configurations
Migrates hardcoded CREDIT_COSTS constants to database
"""
from django.core.management.base import BaseCommand
from igny8_core.business.billing.models import CreditCostConfig
from igny8_core.business.billing.constants import CREDIT_COSTS


class Command(BaseCommand):
    help = 'Initialize credit cost configurations from constants'
    
    def handle(self, *args, **options):
        """Migrate hardcoded costs to database"""
        
        operation_metadata = {
            'clustering': {
                'display_name': 'Auto Clustering',
                'description': 'Group keywords into semantic clusters using AI',
                'unit': 'per_request'
            },
            'idea_generation': {
                'display_name': 'Idea Generation',
                'description': 'Generate content ideas from keyword clusters',
                'unit': 'per_request'
            },
            'content_generation': {
                'display_name': 'Content Generation',
                'description': 'Generate article content using AI',
                'unit': 'per_100_words'
            },
            'image_prompt_extraction': {
                'display_name': 'Image Prompt Extraction',
                'description': 'Extract image prompts from content',
                'unit': 'per_request'
            },
            'image_generation': {
                'display_name': 'Image Generation',
                'description': 'Generate images using AI (DALL-E, Runware)',
                'unit': 'per_image'
            },
            'linking': {
                'display_name': 'Content Linking',
                'description': 'Generate internal links between content',
                'unit': 'per_request'
            },
            'optimization': {
                'display_name': 'Content Optimization',
                'description': 'Optimize content for SEO',
                'unit': 'per_200_words'
            },
            'site_structure_generation': {
                'display_name': 'Site Structure Generation',
                'description': 'Generate complete site blueprint',
                'unit': 'per_request'
            },
            'site_page_generation': {
                'display_name': 'Site Page Generation',
                'description': 'Generate site pages from blueprint',
                'unit': 'per_item'
            },
            'reparse': {
                'display_name': 'Content Reparse',
                'description': 'Reparse and update existing content',
                'unit': 'per_request'
            },
        }
        
        created_count = 0
        updated_count = 0
        
        for operation_type, cost in CREDIT_COSTS.items():
            # Skip legacy aliases
            if operation_type in ['ideas', 'content', 'images']:
                continue
            
            metadata = operation_metadata.get(operation_type, {})
            
            config, created = CreditCostConfig.objects.get_or_create(
                operation_type=operation_type,
                defaults={
                    'credits_cost': cost,
                    'display_name': metadata.get('display_name', operation_type.replace('_', ' ').title()),
                    'description': metadata.get('description', ''),
                    'unit': metadata.get('unit', 'per_request'),
                    'is_active': True
                }
            )
            
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'✅ Created: {config.display_name} - {cost} credits')
                )
            else:
                updated_count += 1
                self.stdout.write(
                    self.style.WARNING(f'⚠️  Already exists: {config.display_name}')
                )
        
        self.stdout.write(
            self.style.SUCCESS(f'\n✅ Complete: {created_count} created, {updated_count} already existed')
        )
