<?php
/**
 * BookiFlex Background Jobs Extension
 *
 * @package    BookiFlex
 * @subpackage BackgroundJobs
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\BackgroundJobs\Service;

use ActionScheduler_QueueRunner;

defined( 'ABSPATH' ) || exit;

/**
 * Queue Runner Service
 *
 * Directly triggers ActionScheduler queue execution.
 */
class QueueRunner
{
    /**
     * Run the ActionScheduler queue
     *
     * @return array Execution metadata
     * @throws \RuntimeException if ActionScheduler is not available
     */
    public function run(): array
    {
        if (!$this->isAvailable()) {
            throw new \RuntimeException('ActionScheduler is not available.');
        }

        $runner = ActionScheduler_QueueRunner::instance();
        $runner->run();

        return [
            'success' => true,
            'timestamp' => time(),
        ];
    }

    /**
     * Check if ActionScheduler is available
     *
     * @return bool True if ActionScheduler is loaded
     */
    public function isAvailable(): bool
    {
        return class_exists('ActionScheduler_QueueRunner')
            && function_exists('as_get_scheduled_actions');
    }
}
