# Generated by Django 5.1.3 on 2024-11-26 13:55

from django.db import migrations, models

from treebeard.numconv import NumConv


def update_team_paths(apps, schema_editor):
    Team = apps.get_model("core", "Team")
    alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    steplen = 5

    # Initialize NumConv with the specified custom alphabet
    converter = NumConv(alphabet)

    nodes = Team.objects.all().order_by("created_at")
    for i, node in enumerate(nodes, 1):
        node.depth = 1  # root nodes are at depth 1

        # Use NumConv to encode the index `i` to a base representation and
        # pad it to the specified step length using the custom alphabet
        node.path = converter.int2str(i).rjust(steplen, alphabet[0])

    if nodes:
        Team.objects.bulk_update(nodes, ["depth", "path"])


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0009_contacts_add_index_on_data_emails'),
    ]

    operations = [
        migrations.AddField(
            model_name='team',
            name='depth',
            field=models.PositiveIntegerField(default=0),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='team',
            name='numchild',
            field=models.PositiveIntegerField(default=0),
        ),
        migrations.AddField(
            model_name='team',
            name='path',
            field=models.CharField(default='', max_length=400, db_collation="C"),
            preserve_default=False,
        ),
        migrations.RunPython(update_team_paths, migrations.RunPython.noop),
        migrations.AlterField(
            model_name="team",
            name="path",
            field=models.CharField(unique=True, max_length=400, db_collation="C"),
        ),
    ]
