<?php

namespace ModernFields\Kernel;

use ModernFields\Vendor\Symfony\Component\DependencyInjection\ContainerBuilder;
use ModernFields\Vendor\Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use ModernFields\Vendor\Symfony\Component\Config\FileLocator;

use ModernFields\WordPress\Interfaces\Hooks;

/**
 * Dependency Injection Container
 * 
 * Initializes and configures the Symfony DI container for the plugin.
 * 
 * @since 1.0.0
 */
class Container
{
  /**
   * The container instance
   * 
   * @var ContainerBuilder|null
   */
  protected ?ContainerBuilder $container = null;


  public function __construct()
  {
    $this->container = new ContainerBuilder();
  }

  /**
   * Get the container instance
   * 
   * @since 1.0.0
   * @return ContainerBuilder
   */
  public function getContainer(): ContainerBuilder
  {
    if (!$this->container->isCompiled()) {
      $this->build();
    }

    return $this->container;
  }

  /**
   * Build the container
   * 
   * @since 1.0.0
   * @return ContainerBuilder
   */
  protected function build(): ContainerBuilder
  {
    $loader = new PhpFileLoader($this->container, new FileLocator(MODERN_FIELDS_PLUGIN_DIR . 'config'));
    $loader->load('services.php');

    $this->container->registerForAutoconfiguration(Hooks::class)
      ->addTag('wordpress.hooks');

    $this->container->compile();

    return $this->container;
  }
}
