# Generated by Django 2.2.24 on 2022-01-19 08:45

from django.db import migrations


def migrate_extra_fields_to_segments(apps, schema_editor):
    Member = apps.get_model('members', 'Member')
    SegmentType = apps.get_model('segments', 'SegmentType')
    Segment = apps.get_model('segments', 'Segment')
    CustomMemberFieldSettings = apps.get_model('members', 'CustomMemberFieldSettings')
    MemberPlatformSettings = apps.get_model('members', 'MemberPlatformSettings')
    CustomMemberField = apps.get_model('members', 'CustomMemberField')

    if CustomMemberFieldSettings.objects.count() == 1:
        MemberPlatformSettings.objects.update(
            create_segments=True,
            enable_segments=True
        )
        department, _ = SegmentType.objects.get_or_create(
            name='Department',
            slug='department',
        )
        for member in Member.objects.all():
            field = CustomMemberField.objects.filter(member=member, field__name='department').first()
            if field:
                segment, _ = Segment.objects.get_or_create(
                    type=department,
                    name=field.value
                )
                member.segments.add(segment)
    if CustomMemberFieldSettings.objects.count() == 4:

        city, _ = SegmentType.objects.get_or_create(
            name='City',
            slug='city',
        )
        country, _ = SegmentType.objects.get_or_create(
            name='Country',
            slug='country',
        )
        for member in Member.objects.all():
            field = CustomMemberField.objects.filter(member=member, field__name='country').first()
            if field:
                my_country, _ = Segment.objects.get_or_create(
                    type=country,
                    name=field.value
                )
                member.segments.add(my_country)
            field = CustomMemberField.objects.filter(member=member, field__name='city').first()
            if field:
                my_city, _ = Segment.objects.get_or_create(
                    type=city,
                    name=field.value
                )
                member.segments.add(my_city)


class Migration(migrations.Migration):

    dependencies = [
        ('segments', '0006_auto_20210914_1134'),
    ]

    operations = [
        migrations.RunPython(
            migrate_extra_fields_to_segments,
            migrations.RunPython.noop
        )
    ]


Migrate custom user fields to segments
Migrate custom user fields to segments
