# Generated by Django 2.2.20 on 2021-08-23 11:59

from django.db import models, migrations
from django.db.models.expressions import Func, F, Value


def _remove_rcce_tag(Project):
    """
    Remove RCCE tag and change it to combination of CEA and HEALTH_PUBLIC
    """
    # Hardcode enum value present this time
    RCCE = 14
    HEALTH_PUBLIC = 4
    CEA = 2

    # Add HEALTH_PUBLIC/CEA if there is RCCE
    for tag in [HEALTH_PUBLIC, CEA]:
        Project.objects\
            .filter(secondary_sectors__contains=[RCCE])\
            .exclude(secondary_sectors__contains=[tag])\
            .update(
                secondary_sectors=Func(
                    F('secondary_sectors'),
                    Value(tag),
                    function='ARRAY_APPEND',
                )
            )

    # Finally remove
    Project.objects.filter(secondary_sectors__contains=[RCCE]).update(
        secondary_sectors=Func(F('secondary_sectors'), Value(RCCE), function='ARRAY_REMOVE')
    )


def remove_rcce_tag(apps, schema_editor):
    Project = apps.get_model('deployments', 'Project')
    _remove_rcce_tag(Project)


class Migration(migrations.Migration):

    dependencies = [
        ('deployments', '0038_auto_20210820_0733'),
    ]

    operations = [
        migrations.RunPython(
            remove_rcce_tag,
            reverse_code=migrations.RunPython.noop,
        ),
    ]
