# Generated by Django 3.2.18 on 2023-02-23 09:53

from django.db import migrations, models
import django.db.models.deletion


EventRequest_Status_DONE = 1


def set_event_speaker_for_existing_events(apps, schema_editor):
    Event = apps.get_model("events", "Event")
    EventSpeakerRequest = apps.get_model("event_requests", "EventSpeakerRequest")

    updated_events = []
    for event_speaker_request in EventSpeakerRequest.objects.select_related(
        "event_speaker", "event_request", "event_request__event"
    ).filter(
        event_request__status=EventRequest_Status_DONE,
        accepted=True,
        event_request__event__isnull=False,
    ):
        event_speaker_request.event_request.event.event_speaker = (
            event_speaker_request.event_speaker
        )
        updated_events.append(event_speaker_request.event_request.event)

    if updated_events:
        Event.objects.bulk_update(updated_events, ("event_speaker",))


class Migration(migrations.Migration):
    dependencies = [
        ("event_requests", "0004_auto_20230227_1554"),
        ("events", "0023_event_location_departement_id"),
    ]

    operations = [
        migrations.AddField(
            model_name="event",
            name="event_speaker",
            field=models.ForeignKey(
                blank=True,
                default=None,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="events",
                related_query_name="event",
                to="event_requests.eventspeaker",
                verbose_name="IntervenantÂ·e",
            ),
        ),
        migrations.RunPython(
            set_event_speaker_for_existing_events, migrations.RunPython.noop
        ),
    ]
