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

declare(strict_types=1);

namespace BookiFlex\Extensions\Onboarding;

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use BookiFlex\Core\Infrastructure\Storage\OptionRepositoryInterface;
use BookiFlex\Core\Service\CptCreator\CptCreatorInterface;
use BookiFlex\Core\Service\DateUnit\DateUnitManager;

defined( 'ABSPATH' ) || exit;

/**
 * Onboarding Extension Service Provider
 *
 * Registers all onboarding-related services in the DI container.
 *
 * @package BookiFlex\Extensions\Onboarding
 * @since   1.0.0
 */
class ServiceProvider implements ServiceProviderInterface
{
    /**
     * Register services in container
     *
     * @param  Container $pimple DI container
     * @return void
     */
    public function register(Container $pimple): void
    {
        // State Manager
        $pimple[Service\OnboardingStateManager::class] = function ($c) {
            return new Service\OnboardingStateManager(
                $c[OptionRepositoryInterface::class]
            );
        };

        // Factories
        $pimple[Factory\CurrencyFactory::class] = function ($c) {
            return new Factory\CurrencyFactory();
        };

        $pimple[Factory\DateUnitPriceFactory::class] = function ($c) {
            return new Factory\DateUnitPriceFactory();
        };

        // Onboarding Init Service
        $pimple[Service\OnboardingInitService::class] = function ($c) {
            return new Service\OnboardingInitService(
                $c[Contract\OnboardingPresetsProviderInterface::class],
                $c[Factory\CurrencyFactory::class],
                $c[CptCreatorInterface::class],
                $c[\BookiFlex\Core\Repository\CancellationPolicyRepository::class],
                $c[\BookiFlex\Core\Repository\PaymentTypeRepository::class]
            );
        };

        // Handlers
        $pimple[Service\QuickSetupHandler::class] = function ($c) {
            return new Service\QuickSetupHandler(
                $c[Contract\OnboardingPresetsProviderInterface::class],
                $c[DateUnitManager::class],
                $c[Factory\DateUnitPriceFactory::class],
                $c->get('settings_manager')
            );
        };

        $pimple[Service\TestDataHandler::class] = function ($c) {
            return new Service\TestDataHandler(
                $c[Contract\OnboardingPresetsProviderInterface::class],
                $c[Service\QuickSetupHandler::class],
                $c[\BookiFlex\Core\Repository\CancellationPolicyRepository::class],
                $c[\BookiFlex\Core\Repository\PaymentTypeRepository::class],
                $c->get('settings_manager')
            );
        };

        $pimple[Service\ManualConfigHandler::class] = function ($c) {
            return new Service\ManualConfigHandler(
                $c[Contract\OnboardingPresetsProviderInterface::class]
            );
        };

        // Main service
        $pimple[Service\OnboardingService::class] = function ($c) {
            return new Service\OnboardingService(
                $c[Service\OnboardingStateManager::class],
                $c[CptCreatorInterface::class],
                [
                    'quick_setup' => $c[Service\QuickSetupHandler::class],
                    'test_data' => $c[Service\TestDataHandler::class],
                    'manual' => $c[Service\ManualConfigHandler::class],
                ]
            );
        };

        // API Components
        $pimple[Api\Controller\OnboardingController::class] = function ($c) {
            return new Api\Controller\OnboardingController(
                $c[Service\OnboardingInitService::class],
                $c[Service\OnboardingService::class],
            );
        };
    }
}
