"""Backfill StudentTask.category per Rule 14.

ACADEMIC: student's own learning (handwriting, reading).
MANAGEMENT: system upkeep (book ingest, KC pipeline, glossary, story).

Standards: Rule 14 (task categories); AERA Std 1.0;
ISO 9001:2015 7.5.3; Google SRE Workbook Ch. 17 (fix-forward with
documented migration).
"""
from django.db import migrations


MANAGEMENT_TYPES = [
    "BOOK_INGEST",
    "KC_DISCOVERY", "KC_TAG", "KC_MISCONCEPTION", "KC_SOCRATIC_LADDER",
    "GLOSSARY", "STORY",
]


def forwards(apps, schema_editor):
    StudentTask = apps.get_model("exams", "StudentTask")
    n = StudentTask.objects.filter(task_type__in=MANAGEMENT_TYPES).update(
        category="MANAGEMENT")
    print("  backfilled", n, "tasks to MANAGEMENT")


def backwards(apps, schema_editor):
    StudentTask = apps.get_model("exams", "StudentTask")
    StudentTask.objects.filter(category="MANAGEMENT").update(category="ACADEMIC")


class Migration(migrations.Migration):
    dependencies = [("exams", "0038_studenttask_category")]
    operations = [migrations.RunPython(forwards, backwards)]
