# Generated by Django 3.2.15 on 2023-04-02 17:57

from django.db import migrations

from ...notifications.threads import ThreadNotifications
from ..hydrators import dehydrate_value


settings = [
    {
        "setting": "watch_started_threads",
        "python_type": "int",
        "dry_value": ThreadNotifications.SITE_AND_EMAIL,
        "is_public": False,
    },
    {
        "setting": "watch_replied_threads",
        "python_type": "int",
        "dry_value": ThreadNotifications.SITE_AND_EMAIL,
        "is_public": False,
    },
    {
        "setting": "watch_new_private_threads_by_followed",
        "python_type": "int",
        "dry_value": ThreadNotifications.SITE_AND_EMAIL,
        "is_public": False,
    },
    {
        "setting": "watch_new_private_threads_by_other_users",
        "python_type": "int",
        "dry_value": ThreadNotifications.SITE_AND_EMAIL,
        "is_public": False,
    },
    {
        "setting": "notify_new_private_threads_by_followed",
        "python_type": "int",
        "dry_value": ThreadNotifications.SITE_AND_EMAIL,
        "is_public": False,
    },
    {
        "setting": "notify_new_private_threads_by_other_users",
        "python_type": "int",
        "dry_value": ThreadNotifications.SITE_AND_EMAIL,
        "is_public": False,
    },
    {
        "setting": "delete_notifications_older_than",
        "python_type": "int",
        "dry_value": 60,
    },
]


def create_settings(apps, _):
    Setting = apps.get_model("misago_conf", "Setting")
    for setting in settings:
        data = setting.copy()
        if "python_type" in data and "dry_value" in data:
            data["dry_value"] = dehydrate_value(data["python_type"], data["dry_value"])

        Setting.objects.create(**setting)


def delete_settings(apps, _):
    Setting = apps.get_model("misago_conf", "Setting")
    Setting.objects.filter(
        setting__in=[setting["setting"] for setting in settings]
    ).delete()


class Migration(migrations.Migration):
    dependencies = [
        ("misago_conf", "0010_add_admin_link_setting"),
    ]

    operations = [
        migrations.RunPython(create_settings, delete_settings),
    ]
