# Generated by Django 5.1.6 on 2025-03-17 17:39

import aleksis.core.mixins
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


def migrate_guardians(apps, schema_editor):
    PersonModel = apps.get_model('core', 'Person')
    RoleModel = apps.get_model('core', 'Role')
    PersonRelationshipModel = apps.get_model('core', 'PersonRelationship')
    GlobalPreferenceModel = apps.get_model("dynamic_preferences", "GlobalPreferenceModel")

    db_alias = schema_editor.connection.alias

    guardian_role, _ = RoleModel.objects.get_or_create(
        managed_by_app_label="core",
        short_name="guardian",
        defaults={"name": "Guardian", "reciprocal_name": "Child"},
    )

    pref = GlobalPreferenceModel.objects.get_or_create(
        section="people",
        name="roles",
        defaults={"raw_value": str(guardian_role.pk)}
    )

    for person in PersonModel.objects.filter(guardians__isnull=False):
        for guardian in person.guardians.all():
            relationship = PersonRelationshipModel.objects.create(
                person=guardian,
                of_person=person,
            )
            relationship.roles.add(guardian_role)


class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
        ('core', '0078_add_role'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]


    operations = [
        migrations.CreateModel(
            name='PersonRelationship',
            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)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.AddField(
            model_name='personrelationship',
            name='of_person',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='core.person'),
        ),
        migrations.AddField(
            model_name='personrelationship',
            name='person',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='core.person'),
        ),
        migrations.AddField(
            model_name='personrelationship',
            name='roles',
            field=models.ManyToManyField(blank=True, related_name='+', to='core.role', verbose_name='Roles'),
        ),
        migrations.AddField(
            model_name='person',
            name='related_persons',
            field=models.ManyToManyField(blank=True, through='core.PersonRelationship', related_name="related_to_persons", to='core.person', verbose_name='Related persons'),
        ),
        migrations.RunPython(migrate_guardians),
    ]
