<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use PDFDraft\Editor;
use PDFDraft\Activation;
use PDFDraft\Admin;
use PDFDraft\App;
use PDFDraft\Install;
use PDFDraft\ScriptStyle;
use PDFDraft\Settings;
use PDFDraft\Uninstall;
use PDFDraft\Contracts\Services\DesignServiceInterface;
use PDFDraft\Contracts\Services\ImageServiceInterface;
use PDFDraft\Contracts\Services\FontsServiceInterface;
use PDFDraft\Contracts\Repositories\DesignRepositoryInterface;
use PDFDraft\Contracts\Cache\CacheInterface;
use PDFDraft\Contracts\Log\LoggerInterface;
use PDFDraft\Services\DesignService;
use PDFDraft\Services\ImageService;
use PDFDraft\Services\FontsService;
use PDFDraft\Services\StorageService;
use PDFDraft\Repositories\DesignRepository;
use PDFDraft\Support\Cache\TransientCache;
use PDFDraft\Support\Log\Logger;
use PDFDraft\Config;
use PDFDraft\Controllers\V1\SettingsController;
use PDFDraft\Controllers\V1\FontFamiliesController;
use PDFDraft\Controllers\V1\ImagesController;
use PDFDraft\PostsToPdf\PostsToPdf;
use PDFDraft\PostsToPdf\Settings as PostsToPdfSettings;
use PDFDraft\PostsToPdf\RestController as PostsToPdfRestController;
use PDFDraft\PostsToPdf\ButtonInjector;
use PDFDraft\PostsToPdf\PostMetaFields;
use PDFDraft\SystemRequirementsChecker;
use Psr\Log\LogLevel;

use function DI\create;
use function DI\get;

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- DI container bootstrap.
$config = new Config();

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- DI container bootstrap.
$definitions = apply_filters(
	'pdfdraft:di:definitions', // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
	array(
		Config::class                    => create( Config::class ),
		LoggerInterface::class           => create( Logger::class )
			->constructor( LogLevel::DEBUG ),
		CacheInterface::class            => create( TransientCache::class )
			->constructor( $config->get( 'cache.prefix', 'pdfdraft_' ) ),

		DesignRepositoryInterface::class => create( DesignRepository::class ),

		DesignServiceInterface::class    => create( DesignService::class )
			->constructor(
				get( DesignRepositoryInterface::class ),
				get( CacheInterface::class ),
				get( LoggerInterface::class )
			),
		ImageServiceInterface::class     => create( ImageService::class )
			->constructor( get( LoggerInterface::class ) ),
		FontsServiceInterface::class     => create( FontsService::class )
			->constructor( get( LoggerInterface::class ) ),

		App::class                       => create( App::class ),
		ScriptStyle::class               => create( ScriptStyle::class ),
		Settings::class                  => create( Settings::class ),
		Activation::class                => create( Activation::class ),
		Uninstall::class                 => create( Uninstall::class ),
		Admin::class                     => create( Admin::class )->constructor( get( Editor::class ) ),
		Install::class                   => create( Install::class ),
		Editor::class                    => create( Editor::class )->constructor( get( FontsServiceInterface::class ) ),

		SettingsController::class        => create( SettingsController::class ),
		FontFamiliesController::class    => create( FontFamiliesController::class )
			->constructor( get( FontsServiceInterface::class ) ),
		ImagesController::class          => create( ImagesController::class )
			->constructor( get( ImageServiceInterface::class ) ),

		StorageService::class            => create( StorageService::class ),

		// Posts to PDF feature
		PostsToPdfSettings::class        => create( PostsToPdfSettings::class ),
		PostMetaFields::class            => create( PostMetaFields::class ),
		PostsToPdfRestController::class  => create( PostsToPdfRestController::class )
			->constructor( get( PostsToPdfSettings::class ) ),
		ButtonInjector::class            => create( ButtonInjector::class )
			->constructor( get( PostsToPdfSettings::class ) ),
		PostsToPdf::class                => create( PostsToPdf::class )
			->constructor(
				get( PostsToPdfSettings::class ),
				get( ButtonInjector::class ),
				get( PostMetaFields::class ),
				get( PostsToPdfRestController::class )
			),
	)
);

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- DI container bootstrap.
$containerBuilder = new \DI\ContainerBuilder();

$containerBuilder->addDefinitions( $definitions );

return $containerBuilder->build();
