import sys

with open('core/admin.py', 'r') as f:
    content = f.read()

old_block = """                                defaults = {}
                                if participation_status_val and participation_status_val.strip():
                                defaults = {}
                                if participation_status_val and participation_status_val.strip():
                                    status_obj, _ = ParticipationStatus.objects.get_or_create(tenant=tenant, name=participation_status_val.strip())
                                    defaults['participation_status'] = status_obj
                                else:
                                    # Default to 'Invited' if not specified
                                    status_obj, _ = ParticipationStatus.objects.get_or_create(tenant=tenant, name='Invited')
                                    defaults['participation_status'] = status_obj"""

new_block = """                                defaults = {}
                                if participation_status_val and participation_status_val.strip():
                                    status_obj, _ = ParticipationStatus.objects.get_or_create(tenant=tenant, name=participation_status_val.strip())
                                    defaults['participation_status'] = status_obj
                                else:
                                    # Default to 'Invited' if not specified
                                    status_obj, _ = ParticipationStatus.objects.get_or_create(tenant=tenant, name='Invited')
                                    defaults['participation_status'] = status_obj"""

if old_block in content:
    new_content = content.replace(old_block, new_block)
    with open('core/admin.py', 'w') as f:
        f.write(new_content)
    print("Patch applied successfully")
else:
    print("Old block not found")
