from datetime import timedelta

from django.db import migrations
from django.utils import timezone


def seed_demo_data(apps, schema_editor):
    Event = apps.get_model("core", "Event")
    Registration = apps.get_model("core", "Registration")

    if Event.objects.exists():
        return

    now = timezone.now()
    events = [
        {
            "title": "Product Launch Breakfast",
            "slug": "product-launch-breakfast",
            "summary": "A polished morning showcase for partners and customers.",
            "description": "Join our organizer team for a concise launch briefing, networking breakfast, and a live product walkthrough.",
            "venue": "Harbor Room, San Francisco",
            "start_at": now + timedelta(days=10, hours=9),
            "end_at": now + timedelta(days=10, hours=12),
            "capacity": 40,
            "registration_opens": now - timedelta(days=2),
            "registration_closes": now + timedelta(days=9),
        },
        {
            "title": "Community Design Workshop",
            "slug": "community-design-workshop",
            "summary": "A collaborative session on event experience and community building.",
            "description": "This hands-on workshop covers attendee journeys, facilitation prompts, and lightweight planning rituals.",
            "venue": "Northstar Studio, Oakland",
            "start_at": now + timedelta(days=16, hours=14),
            "end_at": now + timedelta(days=16, hours=17),
            "capacity": 24,
            "registration_opens": now - timedelta(days=1),
            "registration_closes": now + timedelta(days=15),
        },
        {
            "title": "Founder Evening Meetup",
            "slug": "founder-evening-meetup",
            "summary": "An informal evening gathering with short talks and open networking.",
            "description": "Meet local founders, hear two fast talks, and stay for the community mixer.",
            "venue": "Lumen Loft, San Jose",
            "start_at": now + timedelta(days=25, hours=18),
            "end_at": now + timedelta(days=25, hours=21),
            "capacity": 60,
            "registration_opens": now,
            "registration_closes": now + timedelta(days=24),
        },
    ]

    created = {}
    for payload in events:
        event = Event.objects.create(**payload)
        created[event.slug] = event

    Registration.objects.create(
        event=created["product-launch-breakfast"],
        full_name="Maya Chen",
        email="maya@example.com",
        company="Juniper Labs",
        notes="Interested in partnership opportunities.",
        status="confirmed",
    )
    Registration.objects.create(
        event=created["product-launch-breakfast"],
        full_name="Noah Patel",
        email="noah@example.com",
        company="Signal House",
        notes="Needs wheelchair-accessible seating.",
        status="confirmed",
    )
    Registration.objects.create(
        event=created["community-design-workshop"],
        full_name="Sofia Reyes",
        email="sofia@example.com",
        company="Craft Bureau",
        notes="Bringing one notebook and many questions.",
        status="confirmed",
    )


def clear_demo_data(apps, schema_editor):
    Event = apps.get_model("core", "Event")
    Event.objects.filter(
        slug__in=[
            "product-launch-breakfast",
            "community-design-workshop",
            "founder-evening-meetup",
        ]
    ).delete()


class Migration(migrations.Migration):

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

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