# Generated by Django 3.2.15 on 2022-10-19 16:11

from django.db import migrations, models


def mark_subscriptions_as_unread(apps, schema_editor):
    """Mark the channel subscriptions as unread."""
    CommentReadReport = apps.get_model("chats", "CommentReadReport")
    ChannelSubscription = apps.get_model("chats", "ChannelSubscription")
    for report in CommentReadReport.objects.filter(unread=True):
        comment = report.comment
        if hasattr(comment, "thread"):
            channel = comment.thread.parent_channel
        if hasattr(comment, "channel"):
            channel = comment.parent_channel
        if hasattr(comment, "threadcomment"):
            channel = comment.threadcomment.parent_thread.parent_channel
        ChannelSubscription.objects.filter(
            channel=channel, user=report.user
        ).update(unread=True)


class Migration(migrations.Migration):

    dependencies = [
        ("chats", "0002_thread_previous_thread"),
    ]

    operations = [
        migrations.AddField(
            model_name="channelsubscription",
            name="unread",
            field=models.BooleanField(
                default=False,
                help_text="Are there unread comments for the user in the channel?",
            ),
        ),
        migrations.RunPython(
            mark_subscriptions_as_unread, migrations.RunPython.noop
        ),
    ]
