"""Start the slider with what the homepage already features: the featured article,
the newest magazine issue and a shop product. Edit or remove them in Admin → Homepage hero slides."""
from django.db import migrations


def seed(apps, schema_editor):
    Slide = apps.get_model('core', 'HeroSlide')
    if Slide.objects.exists():
        return
    Article = apps.get_model('articles', 'Article')
    Product = apps.get_model('store', 'Product')
    published = Article.objects.filter(status='published').order_by('-published_at')
    art = published.filter(is_featured=True).first() or published.first()
    if art:
        Slide.objects.create(kind='article', article=art, order=10)
    mag = (Product.objects.filter(is_active=True, product_type='magazine')
           .order_by('-is_featured', '-published_on', '-created_at').first())
    if mag:
        Slide.objects.create(kind='magazine', product=mag, order=20)
    item = Product.objects.filter(is_active=True, product_type__in=['merch', 'nostalgic']).order_by('-is_featured', '-created_at').first()
    if item:
        Slide.objects.create(kind='product', product=item, order=30)


class Migration(migrations.Migration):
    dependencies = [
        ('core', '0022_hero_slides'),
        ('articles', '0011_magazines_and_homepage_blocks'),
        ('store', '0011_delivery_address'),
    ]
    operations = [migrations.RunPython(seed, migrations.RunPython.noop)]
