# Generated by Django 3.2.13 on 2022-07-05 00:49

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


class Migration(migrations.Migration):

    dependencies = [
        ("bookwyrm", "0150_readthrough_stopped_date"),
    ]

    operations = [
        migrations.RemoveField(
            model_name="notification",
            name="related_book",
        ),
        migrations.AddField(
            model_name="notification",
            name="related_list_items",
            field=models.ManyToManyField(
                related_name="notifications", to="bookwyrm.ListItem"
            ),
        ),
        migrations.AddField(
            model_name="notification",
            name="related_reports",
            field=models.ManyToManyField(to="bookwyrm.Report"),
        ),
        migrations.AddField(
            model_name="notification",
            name="related_users",
            field=models.ManyToManyField(
                related_name="notifications", to=settings.AUTH_USER_MODEL
            ),
        ),
        migrations.AlterField(
            model_name="notification",
            name="related_list_item",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="notifications_tmp",
                to="bookwyrm.listitem",
            ),
        ),
        migrations.AlterField(
            model_name="notification",
            name="related_report",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="notifications_tmp",
                to="bookwyrm.report",
            ),
        ),
        migrations.RunSQL(
            sql="""
            INSERT INTO bookwyrm_notification_related_users (notification_id, user_id)
            SELECT id, related_user_id
            FROM bookwyrm_notification
                WHERE bookwyrm_notification.related_user_id IS NOT NULL;

            INSERT INTO bookwyrm_notification_related_list_items (notification_id, listitem_id)
            SELECT id, related_list_item_id
            FROM bookwyrm_notification
                WHERE bookwyrm_notification.related_list_item_id IS NOT NULL;

            INSERT INTO bookwyrm_notification_related_reports (notification_id, report_id)
            SELECT id, related_report_id
            FROM bookwyrm_notification
                WHERE bookwyrm_notification.related_report_id IS NOT NULL;

            """,
            reverse_sql=migrations.RunSQL.noop,
        ),
        migrations.RemoveField(
            model_name="notification",
            name="related_list_item",
        ),
        migrations.RemoveField(
            model_name="notification",
            name="related_report",
        ),
        migrations.RemoveField(
            model_name="notification",
            name="related_user",
        ),
    ]
