# Generated by Django 3.2.23 on 2024-01-30 11:58
import itertools


from django.db import migrations, models
from django.db.models import Q
from funkwhale_api.music import utils


def set_quality_upload(apps, schema_editor):
    Upload = apps.get_model("music", "Upload")
    extension_to_mimetypes = utils.get_extension_to_mimetype_dict()

    # Low quality
    mp3_query = Q(mimetype__in=extension_to_mimetypes["mp3"]) & Q(bitrate__lte=192)

    OpusAACOGG_query = Q(
        mimetype__in=list(
            itertools.chain(
                extension_to_mimetypes["opus"],
                extension_to_mimetypes["ogg"],
                extension_to_mimetypes["aac"],
            )
        )
    ) & Q(bitrate__lte=96)

    low = Upload.objects.filter((mp3_query) | (OpusAACOGG_query))
    low.update(quality=0)

    # medium
    mp3_query = Q(mimetype__in=extension_to_mimetypes["mp3"]) & Q(bitrate__lte=256)
    ogg_query = Q(mimetype__in=extension_to_mimetypes["ogg"]) & Q(bitrate__lte=192)

    aacopus_query = Q(
        mimetype__in=list(
            itertools.chain(
                extension_to_mimetypes["aac"], extension_to_mimetypes["opus"]
            )
        )
    ) & Q(bitrate__lte=128)

    medium = Upload.objects.filter((mp3_query) | (ogg_query) | (aacopus_query)).exclude(
        pk__in=low.values_list("pk", flat=True)
    )
    medium.update(quality=1)

    # high
    mp3_query = Q(mimetype__in=extension_to_mimetypes["mp3"]) & Q(bitrate__lte=320)
    ogg_query = Q(mimetype__in=extension_to_mimetypes["ogg"]) & Q(bitrate__lte=256)
    aac_query = Q(mimetype__in=extension_to_mimetypes["aac"]) & Q(bitrate__lte=288)
    opus_query = Q(mimetype__in=extension_to_mimetypes["opus"]) & Q(bitrate__lte=160)

    high = (
        Upload.objects.filter((mp3_query) | (ogg_query) | (aac_query) | (opus_query))
        .exclude(pk__in=low.values_list("pk", flat=True))
        .exclude(pk__in=medium.values_list("pk", flat=True))
    )
    high.update(quality=2)

    # veryhigh
    opus_query = Q(mimetype__in=extension_to_mimetypes["opus"]) & Q(bitrate__gte=510)

    flacaifaiff_query = Q(
        mimetype__in=list(
            itertools.chain(
                extension_to_mimetypes["flac"],
                extension_to_mimetypes["aif"],
                extension_to_mimetypes["aiff"],
            )
        )
    )
    Upload.objects.filter((opus_query) | (flacaifaiff_query)).exclude(
        pk__in=low.values_list("pk", flat=True)
    ).exclude(pk__in=medium.values_list("pk", flat=True)).exclude(
        pk__in=high.values_list("pk", flat=True)
    ).update(
        quality=3
    )


def skip(apps, schema_editor):
    pass


class Migration(migrations.Migration):
    dependencies = [
        ("music", "0057_auto_20221118_2108"),
    ]

    operations = [
        migrations.AddField(
            model_name="upload",
            name="quality",
            field=models.IntegerField(
                choices=[(0, "low"), (1, "medium"), (2, "high"), (3, "very_high")],
                default=1,
            ),
        ),
        migrations.RunPython(set_quality_upload, skip),
    ]
