# Generated by Django 4.2.29 on 2026-04-28 18:06

from django.db import migrations
from uuid import uuid4


def update_moods_to_apple_list(apps, schema_editor):
    Mood = apps.get_model("moods", "Mood")
    Scrobble = apps.get_model("scrobbles", "Scrobble")

    # Mapping of old mood titles to new adverb titles with mood_type
    # Format: old_title -> (new_title, mood_type)
    mood_mapping = {
        "Anxious": ("Anxiously", "negative"),
        "Fear": ("Fear", "negative"),
        "Sadness": ("Sadly", "negative"),
        "Anger": ("Angrily", "negative"),
        "Surprise": ("Surprisingly", "positive"),
        "Disgust": ("Disgustedly", "negative"),
        "Joy": ("Joyfully", "positive"),
        "Admiration": ("Admiration", "positive"),
        "Adoration": ("Adoration", "positive"),
        "Aesthetic Appreciation": ("Aesthetic Appreciation", "positive"),
        "Amusement": ("Amusedly", "positive"),
        "Awe": ("Amazingly", "positive"),
        "Awkwardness": ("Awkwardness", "negative"),
        "Boredom": ("Boredom", "negative"),
        "Calmness": ("Calmly", "positive"),
        "Confusion": ("Confusion", "negative"),
        "Craving/Desire": ("Craving/Desire", "positive"),
        "Empathetic pain": ("Empathetic pain", "negative"),
        "Entrancement": ("Entrancement", "positive"),
        "Excitement": ("Excitedly", "positive"),
        "Horror": ("Horror", "negative"),
        "Interest": ("Interest", "positive"),
        "Nostalgia": ("Nostalgia", "positive"),
        "Relief": ("Relievedly", "positive"),
        "Romance": ("Romance", "positive"),
        "Satisfaction": ("Satisfiedly", "positive"),
    }

    # Apple moods (adverbs) with their mood_type
    # Tier1 (positive)
    apple_positive = [
        "Amazingly", "Amusedly", "Bravely", "Calmly", "Confidently",
        "Contentedly", "Excitedly", "Gratefully", "Happily", "Hopefully",
        "Joyfully", "Passionately", "Peacefully", "Proudly", "Relievedly",
        "Satisfiedly", "Surprisingly",
    ]
    # Tier2 (negative)
    apple_negative = [
        "Angrily", "Annoyedly", "Anxiously", "Ashamedly", "Disappointedly",
        "Discouragedly", "Disgustedly", "Drainedly", "Embarrassedly",
        "Frustratedly", "Guiltily", "Hopelessly", "Indifferently",
        "Irritatedly", "Jealously", "Lonelily", "Overwhelmingly", "Sadly",
        "Scaredly", "Stressedly", "Worriedly",
    ]

    apple_moods = set(apple_positive + apple_negative)

    # Update existing moods
    for old_title, (new_title, mood_type) in mood_mapping.items():
        try:
            mood = Mood.objects.get(title=old_title)
            mood.title = new_title
            mood.mood_type = mood_type
            mood.save()
        except Mood.DoesNotExist:
            pass

    # Delete moods with no scrobbles that aren't in Apple list
    moods_to_check = Mood.objects.filter(mood_type__isnull=True)
    for mood in moods_to_check:
        if not Scrobble.objects.filter(mood=mood).exists():
            if mood.title not in apple_moods:
                mood.delete()

    # Create missing Apple moods
    for title in apple_positive:
        Mood.objects.get_or_create(
            title=title,
            defaults={"mood_type": "positive", "uuid": uuid4()}
        )

    for title in apple_negative:
        Mood.objects.get_or_create(
            title=title,
            defaults={"mood_type": "negative", "uuid": uuid4()}
        )


class Migration(migrations.Migration):

    dependencies = [
        ("moods", "0006_add_mood_type_to_mood"),
        ("scrobbles", "0057_scrobble_mood_alter_scrobble_log_and_more"),
    ]

    operations = [
        migrations.RunPython(
            update_moods_to_apple_list,
            migrations.RunPython.noop,
        ),
    ]
