# Generated by Django 4.2.16 on 2025-01-17 16:21

from django.db import migrations
from django.db.models import Q


def create_locations(apps):
    Country = apps.get_model("core", "Country")
    Location = apps.get_model("core", "Location")
    countries_with_a_location = set(
        loc.country.code
        for loc in Location.objects.filter(
            name="",
            country__isnull=False,
            geonames_id__isnull=True,
        ).prefetch_related("country")
    )
    countries_to_create = []
    for country in Country.objects.all():
        if country.code not in countries_with_a_location:
            countries_to_create.append(Location(country=country))
    locations = Location.objects.bulk_create(countries_to_create, batch_size=3600)
    locations_by_country_code = {
        loc.country.code: loc for loc in locations if loc.country
    }
    return locations_by_country_code


def create_organizations(apps, accounts, frozen_authors, preprint_authors):
    Organization = apps.get_model("core", "Organization")
    organizations_to_create = []
    for account in accounts:
        organizations_to_create.append(
            Organization(migration_id=f"account_{account.pk}")
        )
    for frozen_author in frozen_authors:
        organizations_to_create.append(
            Organization(migration_id=f"frozen_{frozen_author.pk}")
        )
    for preprint_author in preprint_authors:
        organizations_to_create.append(
            Organization(migration_id=f"preprint_{preprint_author.pk}")
        )
    Organization.objects.bulk_create(organizations_to_create, batch_size=3600)
    orgs_by_migration_id = {org.migration_id: org for org in Organization.objects.all()}
    return orgs_by_migration_id


def migrate_country(
    apps,
    accounts,
    frozen_authors,
    preprint_authors,
    orgs_by_migration_id,
    locations_by_country_code,
):
    Organization = apps.get_model("core", "Organization")
    organization_location_links = {}
    for account in accounts:
        if account.country:
            organization = orgs_by_migration_id[f"account_{account.pk}"]
            location = locations_by_country_code[account.country.code]
            key = f"{organization.pk}_{location.pk}"
            if key not in organization_location_links:
                link = Organization.locations.through(
                    organization_id=organization.pk,
                    location_id=location.pk,
                )
                organization_location_links[key] = link
    for frozen_author in frozen_authors:
        if frozen_author.country:
            organization = orgs_by_migration_id[f"frozen_{frozen_author.pk}"]
            location = locations_by_country_code[frozen_author.country.code]
            key = f"{organization.pk}_{location.pk}"
            if key not in organization_location_links:
                link = Organization.locations.through(
                    organization_id=organization.pk,
                    location_id=location.pk,
                )
                organization_location_links[key] = link
    # Note: The PreprintAuthor object does not need any country data migrated
    Organization.locations.through.objects.bulk_create(
        organization_location_links.values(),
        batch_size=3600,
    )


def migrate_institution(
    apps,
    accounts,
    frozen_authors,
    preprint_authors,
    orgs_by_migration_id,
):
    OrganizationName = apps.get_model("core", "OrganizationName")
    org_names_to_create = []
    for account in accounts:
        organization = orgs_by_migration_id[f"account_{account.pk}"]
        if account.institution:
            org_names_to_create.append(
                OrganizationName(
                    value=account.institution,
                    custom_label_for=organization,
                )
            )
    for frozen_author in frozen_authors:
        organization = orgs_by_migration_id[f"frozen_{frozen_author.pk}"]
        if frozen_author.institution:
            org_names_to_create.append(
                OrganizationName(
                    value=frozen_author.institution,
                    custom_label_for=organization,
                )
            )
    for preprint_author in preprint_authors:
        organization = orgs_by_migration_id[f"preprint_{preprint_author.pk}"]
        org_names_to_create.append(
            OrganizationName(
                value=preprint_author.affiliation,
                custom_label_for=organization,
            )
        )
    OrganizationName.objects.bulk_create(org_names_to_create, batch_size=3600)


def create_affiliations(
    apps,
    accounts,
    frozen_authors,
    preprint_authors,
    orgs_by_migration_id,
):
    ControlledAffiliation = apps.get_model("core", "ControlledAffiliation")
    affiliations_to_create = []
    for account in accounts:
        organization = orgs_by_migration_id[f"account_{account.pk}"]
        affiliations_to_create.append(
            ControlledAffiliation(
                account=account,
                organization=organization,
                department=account.department,
                is_primary=True,
            )
        )
    for frozen_author in frozen_authors:
        organization = orgs_by_migration_id[f"frozen_{frozen_author.pk}"]
        affiliations_to_create.append(
            ControlledAffiliation(
                frozen_author=frozen_author,
                organization=organization,
                department=frozen_author.department,
                is_primary=True,
            )
        )
    for preprint_author in preprint_authors:
        organization = orgs_by_migration_id[f"preprint_{preprint_author.pk}"]
        affiliations_to_create.append(
            ControlledAffiliation(
                preprint_author=preprint_author,
                organization=organization,
                is_primary=True,
            )
        )
    ControlledAffiliation.objects.bulk_create(affiliations_to_create, batch_size=3600)


def remove_migration_ids(apps):
    Organization = apps.get_model("core", "Organization")
    Organization.objects.filter(~Q(migration_id="")).update(migration_id="")


def manage_data_migration(apps, schema_editor):
    Account = apps.get_model("core", "Account")
    FrozenAuthor = apps.get_model("submission", "FrozenAuthor")
    PreprintAuthor = apps.get_model("repository", "PreprintAuthor")

    accounts = Account.objects.filter(
        ~Q(institution__exact="") | ~Q(department__exact="")
    )
    frozen_authors = FrozenAuthor.objects.filter(
        ~Q(institution__exact="") | ~Q(department__exact="")
    )
    preprint_authors = PreprintAuthor.objects.filter(
        affiliation__isnull=False,
    )

    locations_by_country_code = create_locations(apps)
    orgs_by_migration_id = create_organizations(
        apps,
        accounts,
        frozen_authors,
        preprint_authors,
    )
    migrate_country(
        apps,
        accounts,
        frozen_authors,
        preprint_authors,
        orgs_by_migration_id,
        locations_by_country_code,
    )
    migrate_institution(
        apps,
        accounts,
        frozen_authors,
        preprint_authors,
        orgs_by_migration_id,
    )
    create_affiliations(
        apps,
        accounts,
        frozen_authors,
        preprint_authors,
        orgs_by_migration_id,
    )
    remove_migration_ids(apps)


class Migration(migrations.Migration):
    dependencies = [
        ("core", "0104_location_organization_affiliation"),
    ]

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