# Generated by Django 2.2.24 on 2022-06-08 13:13

from django.db import migrations

def create_teams(apps, schema_editor):
    PeriodParticipant = apps.get_model('time_based', 'PeriodParticipant')
    Team = apps.get_model('activities', 'Team')

    status_map = {
        'new': 'new',
        'accepted': 'open',
        'rejected': 'cancelled',
        'cancelled': 'cancelled',
        'withdrawn': 'withdrawn',
        'stopped': 'cancelled'
    }


    for participant in PeriodParticipant.objects.filter(
        team=None,
        activity__team_activity='teams'
    ):
        team, _created = Team.objects.get_or_create(
            activity=participant.activity, 
            owner=participant.user, 
            status=status_map.get(participant.status, 'open')
        )
        participant.team = team

        participant.save()


class Migration(migrations.Migration):

    dependencies = [
        ('activities', '0056_auto_20220406_1220'),
    ]

    operations = [
        migrations.RunPython(create_teams)
    ]

add migration that adds a team to existing team participants