<?php

namespace App\Blog\Service;

use App\Blog\Entity\Blog;
use App\Blog\Repository\BlogRepository;

/**
 * Publishes drafts whose scheduled_at has arrived. A post in the review workflow
 * is published only once review_status = approved — the medical-review gate wins
 * over the schedule, so AI-generated content is never auto-published unreviewed.
 */
class ScheduledBlogPublisher
{
    public function __construct(private readonly BlogRepository $blogRepo) {}

    /** @return int number of posts published */
    public function publishDue(?int $now = null): int
    {
        $now = $now ?? time();
        $due = $this->blogRepo->findDueForPublish($now);
        foreach ($due as $blog) {
            $blog->setStatus(Blog::STATUS_PUBLISHED);
            $this->blogRepo->save($blog);
        }
        return count($due);
    }
}
