# Generated by Django 5.0.7 on 2024-07-16 10:04

import django.db.models.deletion
import django_countries.fields
from django.conf import settings
from django.db import migrations, models

def migrate_addresses(apps, schema_editor):
    Person = apps.get_model("core", "Person")
    Address = apps.get_model("core", "Address")
    AddressType = apps.get_model("core", "AddressType")

    address_type = AddressType.objects.create(name="default", managed_by_app_label="core")

    for p in Person.objects.all():
        if p.street or p.housenumber or p.postal_code or p.place:
            address = Address.objects.create(
                street=p.street,
                housenumber=p.housenumber,
                postal_code=p.postal_code,
                place=p.place,
            )
            address.address_types.add(address_type)

            p.addresses.add(address)

class Migration(migrations.Migration):

    dependencies = [
        ("core", "0074_rename_sex_choices"),
    ]

    operations = [
        migrations.CreateModel(
            name="AddressType",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "managed_by_app_label",
                    models.CharField(
                        blank=True,
                        editable=False,
                        max_length=255,
                        verbose_name="App label of app responsible for managing this instance",
                    ),
                ),
                ("extended_data", models.JSONField(default=dict, editable=False)),
                ("name", models.CharField(max_length=255, verbose_name="Name")),
            ],
            options={
                "verbose_name": "Address Type",
                "verbose_name_plural": "Address Types",
            },
        ),
        migrations.CreateModel(
            name="Address",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "managed_by_app_label",
                    models.CharField(
                        blank=True,
                        editable=False,
                        max_length=255,
                        verbose_name="App label of app responsible for managing this instance",
                    ),
                ),
                ("extended_data", models.JSONField(default=dict, editable=False)),
                (
                    "street",
                    models.CharField(blank=True, max_length=255, verbose_name="Street"),
                ),
                (
                    "housenumber",
                    models.CharField(
                        blank=True, max_length=255, verbose_name="Street number"
                    ),
                ),
                (
                    "postal_code",
                    models.CharField(
                        blank=True, max_length=255, verbose_name="Postal code"
                    ),
                ),
                (
                    "place",
                    models.CharField(blank=True, max_length=255, verbose_name="Place"),
                ),
                ("country", django_countries.fields.CountryField(max_length=2, verbose_name="Country", blank=True)),
                ("address_types", models.ManyToManyField(to="core.addresstype")),
            ],
            options={
                "verbose_name": "Address",
                "verbose_name_plural": "Addresses",
            },
        ),
        migrations.CreateModel(
            name="PersonAddressThrough",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "managed_by_app_label",
                    models.CharField(
                        blank=True,
                        editable=False,
                        max_length=255,
                        verbose_name="App label of app responsible for managing this instance",
                    ),
                ),
                ("extended_data", models.JSONField(default=dict, editable=False)),
                (
                    "address",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE, to="core.address"
                    ),
                ),
                (
                    "person",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE, to="core.person"
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddField(
            model_name="person",
            name="addresses",
            field=models.ManyToManyField(
                blank=True,
                related_name="persons",
                through="core.PersonAddressThrough",
                to="core.address",
                verbose_name="Addresses",
            ),
        ),
        migrations.RunPython(migrate_addresses),
    ]
