# Generated by Django 5.2.9 on 2025-12-19 18:28

from django.db import migrations
from decimal import Decimal


def populate_token_config(apps, schema_editor):
    """
    Populate BillingConfiguration singleton and update CreditCostConfig records.
    Token-based pricing ratios (tokens per 1 credit):
    - Default: 100 tokens = 1 credit
    - AI operations vary by complexity
    """
    BillingConfiguration = apps.get_model('billing', 'BillingConfiguration')
    CreditCostConfig = apps.get_model('billing', 'CreditCostConfig')
    
    # Create BillingConfiguration singleton (if not exists)
    if not BillingConfiguration.objects.exists():
        BillingConfiguration.objects.create(
            default_tokens_per_credit=100,
            default_credit_price_usd=Decimal('0.01'),
            enable_token_based_reporting=True,
            credit_rounding_mode='up'
        )
    
    # Token-to-credit ratios for each operation type
    # Lower number = more expensive (fewer tokens per credit)
    # Higher number = cheaper (more tokens per credit)
    operation_configs = {
        'clustering': {
            'tokens_per_credit': 150,  # Clustering is fairly complex
            'min_credits': 2,
            'price_per_credit_usd': Decimal('0.01'),
            'display_name': 'Content Clustering',
            'description': 'AI-powered keyword clustering'
        },
        'idea_generation': {
            'tokens_per_credit': 200,  # Idea generation is mid-complexity
            'min_credits': 1,
            'price_per_credit_usd': Decimal('0.01'),
            'display_name': 'Idea Generation',
            'description': 'AI content idea generation'
        },
        'content_generation': {
            'tokens_per_credit': 100,  # Content generation is expensive (outputs many tokens)
            'min_credits': 3,
            'price_per_credit_usd': Decimal('0.01'),
            'display_name': 'Content Generation',
            'description': 'AI content writing'
        },
        'image_generation': {
            'tokens_per_credit': 50,  # Image generation is most expensive
            'min_credits': 5,
            'price_per_credit_usd': Decimal('0.02'),
            'display_name': 'Image Generation',
            'description': 'AI image generation'
        },
        'optimization': {
            'tokens_per_credit': 200,  # Optimization is algorithm-based with minimal AI
            'min_credits': 1,
            'price_per_credit_usd': Decimal('0.005'),
            'display_name': 'Content Optimization',
            'description': 'SEO and content optimization'
        },
        'linking': {
            'tokens_per_credit': 300,  # Linking is mostly algorithmic
            'min_credits': 1,
            'price_per_credit_usd': Decimal('0.005'),
            'display_name': 'Internal Linking',
            'description': 'Automatic internal link injection'
        },
        'reparse': {
            'tokens_per_credit': 150,  # Reparse is mid-complexity
            'min_credits': 1,
            'price_per_credit_usd': Decimal('0.01'),
            'display_name': 'Content Reparse',
            'description': 'Content reparsing and analysis'
        },
    }
    
    # Update or create CreditCostConfig records
    for operation_type, config in operation_configs.items():
        CreditCostConfig.objects.update_or_create(
            operation_type=operation_type,
            defaults={
                'tokens_per_credit': config['tokens_per_credit'],
                'min_credits': config['min_credits'],
                'price_per_credit_usd': config['price_per_credit_usd'],
                'display_name': config['display_name'],
                'description': config['description'],
                'is_active': True
            }
        )


def reverse_token_config(apps, schema_editor):
    """Reverse migration - clean up configuration"""
    BillingConfiguration = apps.get_model('billing', 'BillingConfiguration')
    BillingConfiguration.objects.all().delete()
    # Note: We don't delete CreditCostConfig records as they may have historical data


class Migration(migrations.Migration):

    dependencies = [
        ('billing', '0018_remove_creditcostconfig_credits_cost_and_more'),
    ]

    operations = [
        migrations.RunPython(populate_token_config, reverse_token_config),
    ]
