from datetime import date

from django.db import migrations


CATEGORIES = [
    {
        "name": "Learning",
        "slug": "learning",
        "description": "Study sessions, experiments, and small breakthroughs.",
        "accent_color": "#0F766E",
    },
    {
        "name": "Deep Work",
        "slug": "deep-work",
        "description": "Quiet execution blocks that move the hard work forward.",
        "accent_color": "#F97316",
    },
    {
        "name": "Wellbeing",
        "slug": "wellbeing",
        "description": "Energy, routines, and recovery habits that support momentum.",
        "accent_color": "#F59E0B",
    },
]


def seed_demo_data(apps, schema_editor):
    Category = apps.get_model("core", "Category")
    MomentumEntry = apps.get_model("core", "MomentumEntry")

    category_map = {}
    for item in CATEGORIES:
        category, _ = Category.objects.get_or_create(
            slug=item["slug"],
            defaults={
                "name": item["name"],
                "description": item["description"],
                "accent_color": item["accent_color"],
            },
        )
        category_map[item["slug"]] = category

    if MomentumEntry.objects.exists():
        return

    MomentumEntry.objects.bulk_create(
        [
            MomentumEntry(
                category=category_map["learning"],
                title="Finished a Python walkthrough",
                entry_date=date(2026, 4, 10),
                focus_score=8,
                energy_score=7,
                deep_work_minutes=95,
                takeaway="Turned a tutorial into notes I can actually reuse later.",
                reflection="The best part was rewriting the idea in my own words instead of copying line by line.",
            ),
            MomentumEntry(
                category=category_map["deep-work"],
                title="Protected a no-notification build block",
                entry_date=date(2026, 4, 12),
                focus_score=9,
                energy_score=8,
                deep_work_minutes=140,
                takeaway="A single distraction-free block created more progress than a scattered full day.",
                reflection="Turning off notifications before starting made the session feel calm and fast.",
            ),
            MomentumEntry(
                category=category_map["wellbeing"],
                title="Reset the afternoon slump",
                entry_date=date(2026, 4, 14),
                focus_score=6,
                energy_score=8,
                deep_work_minutes=60,
                takeaway="A walk and lighter task list rescued the day instead of writing it off.",
                reflection="I should use recovery intentionally, not only when things already feel off.",
            ),
            MomentumEntry(
                category=category_map["learning"],
                title="Built the first Django tracker flow",
                entry_date=date(2026, 4, 15),
                focus_score=8,
                energy_score=8,
                deep_work_minutes=125,
                takeaway="Shipping a tiny end-to-end slice feels more motivating than polishing ideas in isolation.",
                reflection="Seeing create, list, and detail views together makes the project feel real.",
            ),
        ]
    )


def reverse_seed_demo_data(apps, schema_editor):
    Category = apps.get_model("core", "Category")
    MomentumEntry = apps.get_model("core", "MomentumEntry")
    MomentumEntry.objects.filter(
        title__in=[
            "Finished a Python walkthrough",
            "Protected a no-notification build block",
            "Reset the afternoon slump",
            "Built the first Django tracker flow",
        ]
    ).delete()
    Category.objects.filter(slug__in=["learning", "deep-work", "wellbeing"]).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0001_initial"),
    ]

    operations = [
        migrations.RunPython(seed_demo_data, reverse_seed_demo_data),
    ]
