"""
Generate and publish one blog article (background worker on scheduled day).

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

from django.core.management.base import BaseCommand

from blogs.services.automation_service import BlogAutomationService


class Command(BaseCommand):
    help = "Generate content, images, and publish one article to Shopify."

    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_and_publish_article(article_id)
            self.stdout.write(
                self.style.SUCCESS(
                    f"Article {article_id} published: {article.title} "
                    f"→ {article.shopify_article_url or '(no url)'}"
                )
            )
        except Exception as exc:
            self.stderr.write(self.style.ERROR(str(exc)))
            raise SystemExit(1) from exc
