# Generated by Django 3.2.4 on 2021-06-09 13:10

from django.db import migrations

default_settings_to_migrate = [
    {"variable": "defaultDateFormat", "value": "F d, Y"},
    {"variable": "defaultTimeFormat", "value": "G:i:s"},
    {"variable": "maxMessageLength", "value": "20000"},
    {"variable": "defaultMaxMessages", "value": "15"},
    {"variable": "defaultMaxTopics", "value": "20"},
    {"variable": "oldTopicDays", "value": "120"},
]


def add_default_setting(apps, schema_editor):
    """
    Add default settings on migration
    :param apps:
    :type apps:
    :param schema_editor:
    :type schema_editor:
    """

    Settings = apps.get_model("aa_forum", "Settings")
    db_alias = schema_editor.connection.alias

    default_settings = []
    for default_setting in default_settings_to_migrate:
        default_settings.append(
            Settings(
                variable=default_setting["variable"], value=default_setting["value"]
            )
        )

    Settings.objects.using(db_alias).bulk_create(default_settings)


def remove_default_settings(apps, schema_editor):
    """
    Remove default settings on migration to zero
    :param apps:
    :type apps:
    :param schema_editor:
    :type schema_editor:
    """

    Settings = apps.get_model("aa_forum", "Settings")
    db_alias = schema_editor.connection.alias

    for default_setting in default_settings_to_migrate:
        Settings.objects.using(db_alias).filter(
            variable=default_setting["variable"]
        ).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("aa_forum", "0002_reserved_slugs"),
    ]

    operations = [
        migrations.RunPython(add_default_setting, remove_default_settings),
    ]
