<?php

declare(strict_types=1);

use KimaAI\Addons\AddonManager;
use KimaAI\Admin\AdminTheme;
use KimaAI\Admin\AdminInit;
use KimaAI\Admin\Assets;
use KimaAI\Admin\DebugInfo;
use KimaAI\Admin\Integrations\IntegrationManager;
use KimaAI\Admin\Integrations\WooCommerceIntegration;
use KimaAI\Admin\Settings;
use KimaAI\Admin\Wizard\Wizard;
use KimaAI\Ai\AiService;
use KimaAI\Ai\EnvResolver;
use KimaAI\Ai\Playground;
use KimaAI\Ai\RestApi;
use KimaAI\Ai\Security;
use KimaAI\ChatBot\ChatBot;
use KimaAI\ChatBot\ChatbotAdmin;
use KimaAI\ChatBot\ChatbotAssets;
use KimaAI\ChatBot\ChatbotFeedback;
use KimaAI\ChatBot\ChatbotNormalRequestRunner;
use KimaAI\ChatBot\ChatbotProviderErrorNormalizer;
use KimaAI\ChatBot\ChatbotRequestPreparer;
use KimaAI\ChatBot\ChatbotSseEmitter;
use KimaAI\ChatBot\ChatbotSessionRetention;
use KimaAI\ChatBot\ChatbotStreamingRequestRunner;
use KimaAI\ChatBot\Tools\ChatbotToolRegistry;
use KimaAI\ChatBot\Tools\ToolCallOrchestrator;
use KimaAI\ChatBot\Tools\ToolCommandHandler;
use KimaAI\ChatBot\Tools\ToolContinuationBuilder;
use KimaAI\Helper;
use KimaAI\Lib\EagerServices;
use KimaAI\Loader;
use KimaAI\Models\ChatCsatModel;
use KimaAI\Models\ChatFeedbackAdminModel;
use KimaAI\Models\ChatFeedbackModel;
use KimaAI\Models\ChatModel;
use KimaAI\Models\ImageModel;
use KimaAI\Models\LinkSuggestionModel;
use KimaAI\Surfaces\ClassesSurface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $config): void {
  $services = $config->services()
  ->defaults()
  ->autowire()
  ->autoconfigure()
  ->public();

  // Surfaces
  $services->set(ClassesSurface::class)
  ->arg('$container', service('service_container'));

  // Core services that make up the plugin
  $services->set(Settings::class);
  $services->set(AdminTheme::class);
  $services->set(Wizard::class);
  $services->set(Helper::class);
  $services->set(Assets::class);
  $services->set(DebugInfo::class);
  $services->set(AdminInit::class);
  $services->set(EnvResolver::class);
  $services->set(AiService::class);
  $services->set(RestApi::class);
  $services->set(Security::class);
  $services->set(ChatBot::class);
  $services->set(ChatbotAdmin::class);
  $services->set(ChatbotAssets::class);
  $services->set(ChatbotFeedback::class);
  $services->set(ChatbotNormalRequestRunner::class);
  $services->set(ChatbotProviderErrorNormalizer::class);
  $services->set(ChatbotRequestPreparer::class);
  $services->set(ChatbotSessionRetention::class);
  $services->set(ChatbotStreamingRequestRunner::class);
  $services->set(ChatbotSseEmitter::class);
  $services->set(ChatbotToolRegistry::class);
  $services->set(ToolCallOrchestrator::class);
  $services->set(ToolCommandHandler::class);
  $services->set(ToolContinuationBuilder::class);
  $services->set(Playground::class);
  $services->set(ChatModel::class);
  $services->set(ChatCsatModel::class);
  $services->set(ChatFeedbackModel::class);
  $services->set(ChatFeedbackAdminModel::class);
  $services->set(ImageModel::class);
  $services->set(LinkSuggestionModel::class);
  $services->set(IntegrationManager::class);
  $services->set(WooCommerceIntegration::class);

  // Eager initializer: force specific services to be instantiated
  $services->set(EagerServices::class)
  ->arg('$container', service('service_container'))
  ->arg(
    '$services_to_warm',
    [
      AdminInit::class,
      AiService::class,
      ChatBot::class,
    ]
  );

  $services->set(AddonManager::class);

  // Loader – it drives the boot sequence
  $services->set(Loader::class)
  ->arg('$container', service('service_container'))
  ->call('registerInitializer', [EagerServices::class])
  ->call('registerIntegration', [AdminInit::class])
  ->call('registerIntegration', [Assets::class])
  ->call('registerIntegration', [DebugInfo::class])
  ->call('registerIntegration', [Wizard::class])
  ->call('registerIntegration', [ChatbotAssets::class])
  ->call('registerIntegration', [ChatbotSessionRetention::class])
  ->call('registerIntegration', [ToolCallOrchestrator::class])
  ->call('registerIntegration', [Security::class])
  ->call('registerRoute', [RestApi::class])
  ->call('registerRoute', [AdminTheme::class])
  ->call('registerRoute', [Wizard::class])
  ->call('registerRoute', [ChatBot::class])
  ->call('registerRoute', [ChatbotAdmin::class])
  ->call('registerRoute', [ChatbotFeedback::class])
  ->call('registerRoute', [Playground::class]);
};
