# Generated by Django 2.2.16 on 2021-03-24 11:38

from itertools import groupby

from django.db import migrations, models

from ..models import AccessAudit, HttpAccept, NavigationEventAudit, UserAgent, ViewName

model_map = {
    HttpAccept: [AccessAudit],
    UserAgent: [AccessAudit, NavigationEventAudit],
    ViewName: [NavigationEventAudit],
}
field_map = {
    HttpAccept: "http_accept_fk_id",
    UserAgent: "user_agent_fk_id",
    ViewName: "view_fk_id",
}


def delete_duplicates(model):
    def update_dups(rel_model, first_id, other_ids):
        field_name = field_map[model]
        rel_model.objects.filter(
            **{field_name + "__in": other_ids}
        ).update(
            **{field_name: first_id},
        )

    def do_delete(apps, schema_editor):
        def sort_key(item):
            id, value = item
            return value

        dup_values = list(
            model.objects
            .values("value")
            .annotate(value_count=models.Count("value"))
            .filter(value_count__gt=1)
            .values_list("value", flat=True)
        )
        dups = (
            model.objects
            .filter(value__in=dup_values)
            .values_list("id", "value")
        )
        for value, pairs in groupby(sorted(dups, key=sort_key), key=sort_key):
            ids = sorted(id for id, value in pairs)
            first_id, *other_ids = ids
            for rel_model in model_map[model]:
                update_dups(rel_model, first_id, other_ids)
            model.objects.filter(value=value, id__in=other_ids).delete()

    return do_delete


class Migration(migrations.Migration):

    atomic = False
    dependencies = [
        ('auditcare', '0001_sqlmodels'),
    ]

    operations = [
        migrations.RunPython(delete_duplicates(HttpAccept), lambda *a: None),
        migrations.AlterField(
            model_name='httpaccept',
            name='value',
            field=models.CharField(db_index=True, max_length=255, unique=True),
        ),
        migrations.RunPython(delete_duplicates(UserAgent), lambda *a: None),
        migrations.AlterField(
            model_name='useragent',
            name='value',
            field=models.CharField(db_index=True, max_length=255, unique=True),
        ),
        migrations.RunPython(delete_duplicates(ViewName), lambda *a: None),
        migrations.AlterField(
            model_name='viewname',
            name='value',
            field=models.CharField(db_index=True, max_length=255, unique=True),
        ),
    ]

# Generated by Django 2.2.16 on 2021-03-24 11:38

from itertools import groupby

from django.db import migrations, models

from ..models import AccessAudit, HttpAccept, NavigationEventAudit, UserAgent, ViewName

model_map = {
    HttpAccept: [AccessAudit],
    UserAgent: [AccessAudit, NavigationEventAudit],
    ViewName: [NavigationEventAudit],
}
field_map = {
    HttpAccept: "http_accept_fk_id",
    UserAgent: "user_agent_fk_id",
    ViewName: "view_fk_id",
}


def delete_duplicates(model):
    def update_dups(rel_model, first_id, other_ids):
        field_name = field_map[model]
        rel_model.objects.filter(
            **{field_name + "__in": other_ids}
        ).update(
            **{field_name: first_id},
        )

    def do_delete(apps, schema_editor):
        def sort_key(item):
            id, value = item
            return value

        dup_values = list(
            model.objects
            .values("value")
            .annotate(value_count=models.Count("value"))
            .filter(value_count__gt=1)
            .values_list("value", flat=True)
        )
        dups = (
            model.objects
            .filter(value__in=dup_values)
            .values_list("id", "value")
        )
        for value, pairs in groupby(sorted(dups, key=sort_key), key=sort_key):
            ids = sorted(id for id, value in pairs)
            first_id, *other_ids = ids
            for rel_model in model_map[model]:
                update_dups(rel_model, first_id, other_ids)
            model.objects.filter(value=value, id__in=other_ids).delete()

    return do_delete


class Migration(migrations.Migration):

    atomic = False
    dependencies = [
        ('auditcare', '0001_sqlmodels'),
    ]

    operations = [
        migrations.RunPython(delete_duplicates(HttpAccept), lambda *a: None),
        migrations.AlterField(
            model_name='httpaccept',
            name='value',
            field=models.CharField(db_index=True, max_length=255, unique=True),
        ),
        migrations.RunPython(delete_duplicates(UserAgent), lambda *a: None),
        migrations.AlterField(
            model_name='useragent',
            name='value',
            field=models.CharField(db_index=True, max_length=255, unique=True),
        ),
        migrations.RunPython(delete_duplicates(ViewName), lambda *a: None),
        migrations.AlterField(
            model_name='viewname',
            name='value',
            field=models.CharField(db_index=True, max_length=255, unique=True),
        ),
    ]

# Generated by Django 2.2.16 on 2021-03-24 11:38