# Generated migration to add payment constraints

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('billing', '0010_add_database_constraints'),
    ]

    operations = [
        # Add composite unique constraint on manual_reference + tenant_id
        # This prevents duplicate payment submissions with same reference for an account
        migrations.RunSQL(
            sql="""
            CREATE UNIQUE INDEX billing_payment_manual_ref_account_unique 
            ON igny8_payments(tenant_id, manual_reference) 
            WHERE manual_reference != '' AND manual_reference IS NOT NULL;
            """,
            reverse_sql="""
            DROP INDEX IF EXISTS billing_payment_manual_ref_account_unique;
            """
        ),
    ]
