# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Generated by Django 3.2.6 on 2021-09-30 18:38

from django.db import migrations
from django.db.models import Case, Value, When

METRIC_ALL = 1
METRIC_ALL_WORDS = 2
METRIC_TRANSLATED = 3
METRIC_TRANSLATED_WORDS = 4
METRIC_APPROVED = 5
METRIC_APPROVED_WORDS = 6
METRIC_ALLCHECKS = 7
METRIC_ALLCHECKS_WORDS = 8
METRIC_DISMISSED_CHECKS = 9
METRIC_DISMISSED_CHECKS_WORDS = 10
METRIC_SUGGESTIONS = 11
METRIC_SUGGESTIONS_WORDS = 12
METRIC_COMMENTS = 13
METRIC_COMMENTS_WORDS = 14
METRIC_LANGUAGES = 15
METRIC_SOURCE_STRINGS = 16
METRIC_SOURCE_WORDS = 17
METRIC_CHANGES = 18
METRIC_MEMORY = 19
METRIC_USERS = 20
METRIC_CONTRIBUTORS = 21
METRIC_PROJECTS = 22
METRIC_SCREENSHOTS = 23
METRIC_COMPONENTS = 24
METRIC_TRANSLATIONS = 25
METRIC_MACHINERY_INTERNAL = 26
METRIC_MACHINERY_EXTERNAL = 27

METRIC_IDS = {
    "all": METRIC_ALL,
    "all_words": METRIC_ALL_WORDS,
    "translated": METRIC_TRANSLATED,
    "translated_words": METRIC_TRANSLATED_WORDS,
    "approved": METRIC_APPROVED,
    "approved_words": METRIC_APPROVED_WORDS,
    "allchecks": METRIC_ALLCHECKS,
    "allchecks_words": METRIC_ALLCHECKS_WORDS,
    "dismissed_checks": METRIC_DISMISSED_CHECKS,
    "dismissed_checks_words": METRIC_DISMISSED_CHECKS_WORDS,
    "suggestions": METRIC_SUGGESTIONS,
    "suggestions_words": METRIC_SUGGESTIONS_WORDS,
    "comments": METRIC_COMMENTS,
    "comments_words": METRIC_COMMENTS_WORDS,
    "languages": METRIC_LANGUAGES,
    "source_strings": METRIC_SOURCE_STRINGS,
    "source_words": METRIC_SOURCE_WORDS,
    "changes": METRIC_CHANGES,
    "memory": METRIC_MEMORY,
    "users": METRIC_USERS,
    "contributors": METRIC_CONTRIBUTORS,
    "projects": METRIC_PROJECTS,
    "screenshots": METRIC_SCREENSHOTS,
    "components": METRIC_COMPONENTS,
    "translations": METRIC_TRANSLATIONS,
    "machinery:internal": METRIC_MACHINERY_INTERNAL,
    "machinery:external": METRIC_MACHINERY_EXTERNAL,
}


def migrate_kind(apps, schema_editor):
    Metric = apps.get_model("metrics", "Metric")
    db_alias = schema_editor.connection.alias
    conditions = [
        When(name=name, then=Value(kind)) for name, kind in METRIC_IDS.items()
    ]

    Metric.objects.using(db_alias).update(kind=Case(*conditions))


class Migration(migrations.Migration):
    dependencies = [
        ("metrics", "0011_metric_kind"),
    ]

    operations = [
        migrations.RunPython(
            migrate_kind, migrations.RunPython.noop, elidable=False, atomic=False
        )
    ]
