<?php

namespace AutoAdmin\Bundle\Admin\Factory;

use AutoAdmin\Bundle\Admin\Builder\LayoutBuilder;
use AutoAdmin\Bundle\Admin\Builder\TableBuilder;
use AutoAdmin\Bundle\Admin\Configuration\ConfigManager;
use AutoAdmin\Bundle\Admin\Helper\AccessHelper;
use AutoAdmin\Bundle\Admin\Helper\EntityHelper;
use AutoAdmin\Bundle\Admin\Helper\StringifierHelper;
use AutoAdmin\Bundle\Admin\Router\AdminRouter;
use AutoAdmin\Bundle\Admin\Search\EntitySearchHelper;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\HttpFoundation\RequestStack;

class BuilderFactory
{
    public function __construct(
        protected EntityManagerInterface $entityManager,
        protected ConfigManager $configManager,
        protected EntityHelper $entityHelper,
        protected AdminRouter $adminRouter,
        protected AccessHelper $accessHelper,
        protected StringifierHelper $stringifierHelper,
        protected EntitySearchHelper $entitySearchHelper,
        protected RequestStack $requestStack,
    ) {
    }

    public function getTableBuilder(): TableBuilder
    {
        return $this->get('table');
    }

    /**
     * @throws Exception
     */
    public function get(string $builderName)
    {
        $className = 'AutoAdmin\\Bundle\Admin\\Builder\\'.ucfirst($builderName).'Builder';

        if (!class_exists($className)) {
            throw new Exception($className." doesn't exist");
        }

        return new $className(
            $this->entityManager,
            $this->configManager,
            $this->entityHelper,
            $this->adminRouter,
            $this->accessHelper,
            $this->stringifierHelper,
            $this->entitySearchHelper,
            $this->requestStack,
        );
    }

    public function getLayoutBuilder(): LayoutBuilder
    {
        return $this->get('layout');
    }
}
