# Generated by Django 4.2.7 on 2024-01-05 10:27

from django.db import migrations

def forwards_func(apps, schema_editor):
    ObstacleGroup = apps.get_model('mapdata', 'ObstacleGroup')
    Obstacle = apps.get_model('mapdata', 'Obstacle')
    LineObstacle = apps.get_model('mapdata', 'LineObstacle')

    grouped = {}

    for obstacle in Obstacle.objects.filter(color__isnull=False):
        grouped.setdefault(obstacle.color, []).append(obstacle)

    for lineobstacle in LineObstacle.objects.filter(color__isnull=False):
        grouped.setdefault(lineobstacle.color, []).append(lineobstacle)

    for color, obstacles in grouped.items():
        group = ObstacleGroup.objects.create(
            color=color,
            titles={"en": f"Color {color}"}
        )
        for obstacle in obstacles:
            obstacle.group = group
            obstacle.save()


def backwards_func(apps, schema_editor):
    ObstacleGroup = apps.get_model('mapdata', 'ObstacleGroup')
    ObstacleGroup.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('mapdata', '0099_theming'),
    ]

    operations = [
        migrations.RunPython(forwards_func, backwards_func)
    ]
