"""
Generate one blog article in the foreground (used by background worker).

Usage:
  python manage.py process_blog_article <ARTICLE_ID>
"""

from django.core.management.base import BaseCommand

from blogs.services.automation_service import BlogAutomationService


class Command(BaseCommand):
    help = "Generate content and images for one blog article (worker)."

    def add_arguments(self, parser):
        parser.add_argument("article_id", type=int)

    def handle(self, *args, **options):
        article_id = options["article_id"]
        try:
            article = BlogAutomationService.process_article(article_id)
            self.stdout.write(
                self.style.SUCCESS(
                    f"Article {article_id} ready: {article.title} "
                    f"(scheduled {article.scheduled_date})"
                )
            )
        except Exception as exc:
            self.stderr.write(self.style.ERROR(str(exc)))
            raise SystemExit(1) from exc
