# Generated by Django 5.2.4 on 2026-05-25 15:08

from django.db import migrations
import json

TABLES_TO_ADD = [
    "tt_activity_type",
    "tt_module_group_module",
    "tt_student_set_activity",
]

def add_more_import_tables(apps, schema_editor):
    TtSetting = apps.get_model("api", "TtSetting")

    setting = TtSetting.objects.filter(param="import_tables").first()
    if not setting:
        return

    try:
        tables = json.loads(setting.value)
    except Exception:
        tables = []

    changed = False

    for table in TABLES_TO_ADD:
        if table not in tables:
            tables.append(table)
            changed = True

    if changed:
        setting.value = json.dumps(tables)
        setting.save()


def reverse_add_more_import_tables(apps, schema_editor):
    TtSetting = apps.get_model("api", "TtSetting")

    setting = TtSetting.objects.filter(param="import_tables").first()
    if not setting:
        return

    try:
        tables = json.loads(setting.value)
    except Exception:
        tables = []

    changed = False

    for table in TABLES_TO_ADD:
        if table in tables:
            tables.remove(table)
            changed = True

    if changed:
        setting.value = json.dumps(tables)
        setting.save()


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0068_alter_role_name_alter_ttacademicterm_name_and_more'),
    ]

    operations = [
        migrations.RunPython(
            add_more_import_tables,
            reverse_add_more_import_tables,
        ),
    ]
