<?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;

use BookiFlex\Core\Infrastructure\Storage\CacheRepositoryInterface;
use BookiFlex\Extensions\BackgroundJobs\Api\Controller\CronController;
use BookiFlex\Extensions\BackgroundJobs\Service\FallbackRunner;
use BookiFlex\Extensions\BackgroundJobs\Service\HealthChecker;
use BookiFlex\Extensions\BackgroundJobs\Service\QueueRunner;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

defined( 'ABSPATH' ) || exit;

/**
 * Service Provider
 *
 * Registers all BackgroundJobs services in the DI container.
 */
class ServiceProvider implements ServiceProviderInterface
{
    /**
     * Register services in the container
     *
     * @param Container $pimple DI container
     */
    public function register(Container $pimple): void
    {
        // QueueRunner - triggers ActionScheduler execution
        $pimple[QueueRunner::class] = function ($c) {
            return new QueueRunner();
        };

        // HealthChecker - monitors queue status
        $pimple[HealthChecker::class] = function ($c) {
            return new HealthChecker(
                $c->get(CacheRepositoryInterface::class)
            );
        };

        // FallbackRunner - shutdown hook handler
        $pimple[FallbackRunner::class] = function ($c) {
            return new FallbackRunner(
                $c->get(QueueRunner::class),
                $c->get(HealthChecker::class),
                $c->get(CacheRepositoryInterface::class)
            );
        };

        // CronController - REST API controller
        $pimple[CronController::class] = function ($c) {
            return new CronController(
                $c->get(QueueRunner::class),
                $c->get(HealthChecker::class),
                $c->get(CacheRepositoryInterface::class)
            );
        };
    }
}
