# Generated by Django 3.2.15 on 2022-10-17 07:48

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


def relate_threads(apps, schema_editor):
    Channel = apps.get_model("chats", "Channel")
    for channel in Channel.objects.all():
        previous_thread = None
        for i, thread in enumerate(
            channel.thread_set.all().order_by("date_created")
        ):
            if i:
                thread.previous_thread = previous_thread
                thread.save()
            previous_thread = thread


class Migration(migrations.Migration):

    dependencies = [
        ("chats", "0001_initial"),
    ]

    operations = [
        migrations.AddField(
            model_name="thread",
            name="previous_thread",
            field=models.OneToOneField(
                blank=True,
                help_text="Previous thread in the channel.",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="next_thread",
                to="chats.thread",
            ),
        ),
        migrations.RunPython(relate_threads, migrations.RunPython.noop),
    ]
