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


def seed_newsroom(apps, schema_editor):
    Topic = apps.get_model('core', 'Topic')
    NewsSource = apps.get_model('core', 'NewsSource')
    Article = apps.get_model('core', 'Article')

    topics = [
        ('Artificial Intelligence', 'artificial-intelligence', 'Model launches, research, tooling, and applied AI.', '#00c2a8'),
        ('Startups', 'startups', 'Founders, launches, product momentum, and operator notes.', '#ff6b4a'),
        ('Venture Capital', 'venture-capital', 'Funding rounds, investors, and deal flow.', '#7df9d7'),
        ('Product & Cloud', 'product-cloud', 'SaaS, developer platforms, cloud infrastructure, and enterprise software.', '#ffd166'),
        ('Hardware', 'hardware', 'Devices, chips, robotics, and the physical layer of tech.', '#8ec5ff'),
        ('Security', 'security', 'Cybersecurity, privacy, and resilience across the stack.', '#ff8fab'),
    ]
    for name, slug, description, accent_color in topics:
        Topic.objects.update_or_create(
            slug=slug,
            defaults={
                'name': name,
                'description': description,
                'accent_color': accent_color,
            },
        )

    sources = [
        ('TechCrunch', 'techcrunch', 'https://techcrunch.com', 'https://techcrunch.com/feed/', 'Startup and technology reporting.'),
        ('The Verge', 'the-verge', 'https://www.theverge.com', 'https://www.theverge.com/rss/index.xml', 'Product, platform, and consumer tech news.'),
        ('Wired', 'wired', 'https://www.wired.com', 'https://www.wired.com/feed/rss', 'Culture, technology, and future-of-tech coverage.'),
        ('Ars Technica', 'ars-technica', 'https://arstechnica.com', 'https://feeds.arstechnica.com/arstechnica/index', 'Deep reporting across science, policy, and hardware.'),
    ]
    for name, slug, site_url, feed_url, description in sources:
        NewsSource.objects.update_or_create(
            slug=slug,
            defaults={
                'name': name,
                'site_url': site_url,
                'feed_url': feed_url,
                'description': description,
                'is_active': True,
            },
        )

    topic = Topic.objects.filter(slug='startups').first()
    Article.objects.update_or_create(
        slug='inside-signal-how-to-run-a-fast-tech-newsroom',
        defaults={
            'title': 'Inside Signal: how to run a fast, future-ready tech newsroom',
            'excerpt': 'A seeded original story so the editorial flow is visible even before the first RSS sync completes.',
            'content': 'Signal combines live feed aggregation with a lightweight publishing flow. Editors can scan imported stories, publish original analysis, and shape the front page with featured picks. This seeded story exists to make the first delivery feel complete while you continue building the full magazine.',
            'article_kind': 'original',
            'topic': topic,
            'author_name': 'Signal Editorial Desk',
            'published_at': timezone.now(),
            'dedupe_key': 'seeded-signal-editorial-launch-story',
            'is_featured': True,
            'is_published': True,
        },
    )


def unseed_newsroom(apps, schema_editor):
    Topic = apps.get_model('core', 'Topic')
    NewsSource = apps.get_model('core', 'NewsSource')
    Article = apps.get_model('core', 'Article')
    Article.objects.filter(dedupe_key='seeded-signal-editorial-launch-story').delete()
    NewsSource.objects.filter(slug__in=['techcrunch', 'the-verge', 'wired', 'ars-technica']).delete()
    Topic.objects.filter(slug__in=[
        'artificial-intelligence',
        'startups',
        'venture-capital',
        'product-cloud',
        'hardware',
        'security',
    ]).delete()


class Migration(migrations.Migration):

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

    operations = [
        migrations.RunPython(seed_newsroom, unseed_newsroom),
    ]
