# Migration to add missing payment method configurations
from django.db import migrations


def add_missing_payment_methods(apps, schema_editor):
    """Add stripe and paypal global configs (disabled) and ensure all configs exist"""
    PaymentMethodConfig = apps.get_model('billing', 'PaymentMethodConfig')
    
    # Add global Stripe (disabled - waiting for integration)
    PaymentMethodConfig.objects.get_or_create(
        country_code='*',
        payment_method='stripe',
        defaults={
            'is_enabled': False,
            'display_name': 'Credit/Debit Card (Stripe)',
            'instructions': 'Stripe payment integration coming soon.',
            'sort_order': 1
        }
    )
    
    # Add global PayPal (disabled - waiting for integration)
    PaymentMethodConfig.objects.get_or_create(
        country_code='*',
        payment_method='paypal',
        defaults={
            'is_enabled': False,
            'display_name': 'PayPal',
            'instructions': 'PayPal payment integration coming soon.',
            'sort_order': 2
        }
    )
    
    # Ensure global bank_transfer exists with good instructions
    PaymentMethodConfig.objects.get_or_create(
        country_code='*',
        payment_method='bank_transfer',
        defaults={
            'is_enabled': True,
            'display_name': 'Bank Transfer',
            'instructions': 'Bank transfer details will be provided after registration.',
            'sort_order': 3
        }
    )
    
    # Add manual payment as global option
    PaymentMethodConfig.objects.get_or_create(
        country_code='*',
        payment_method='manual',
        defaults={
            'is_enabled': True,
            'display_name': 'Manual Payment (Contact Support)',
            'instructions': 'Contact support@igny8.com for manual payment arrangements.',
            'sort_order': 10
        }
    )
    
    print("Added/updated payment method configurations")


def remove_added_payment_methods(apps, schema_editor):
    """Reverse migration"""
    PaymentMethodConfig = apps.get_model('billing', 'PaymentMethodConfig')
    PaymentMethodConfig.objects.filter(
        country_code='*',
        payment_method__in=['stripe', 'paypal', 'manual']
    ).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('billing', '0008_add_invoice_subscription_fk'),
    ]

    operations = [
        migrations.RunPython(
            add_missing_payment_methods,
            reverse_code=remove_added_payment_methods
        ),
    ]
