<?php

namespace AutoAdmin\Bundle\Admin\Builder;

use AutoAdmin\Bundle\Admin\Configuration\ConfigManager;
use AutoAdmin\Bundle\Admin\Exception\UnknownActionException;
use AutoAdmin\Bundle\Admin\Exception\UnknownEntityException;
use AutoAdmin\Bundle\Admin\Helper\AccessHelper;
use AutoAdmin\Bundle\Admin\Helper\EntityHelper;
use AutoAdmin\Bundle\Admin\Router\AdminRouter;
use Doctrine\ORM\EntityManager;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class LayoutBuilder
{
    private EntityManager $entityManager;
    private ConfigManager $configManager; // Useless ? :)
    private EntityHelper $entityHelper;
    private PropertyAccessor $accessor;
    private AdminRouter $adminRouter;
    private AccessHelper $accessHelper;

    public function __construct(
        EntityManager $entityManager,
        ConfigManager $configManager,
        EntityHelper  $entityHelper,
        AdminRouter   $adminRouter,
        AccessHelper  $accessHelper
    )
    {
        $this->entityManager = $entityManager;
        $this->configManager = $configManager;
        $this->entityHelper = $entityHelper;
        $this->adminRouter = $adminRouter;

        $this->accessor = PropertyAccess::createPropertyAccessor();
        $this->accessHelper = $accessHelper;
    }

    /**
     * @throws UnknownEntityException
     * @throws UnknownActionException
     */
    public function buildTopbarActions(string|object $entity, string $action = null): mixed
    {
        $class = $this->entityHelper->guessFqcn($entity);
        $entityConfig = $this->entityHelper->getEntityConfig($class);
        if ($action === null) {
            $actions = $this->accessor->getValue($entityConfig, '[topbar][actions]');
        } else {
            $actions = $this->accessor->getValue($entityConfig, '[' . $action . '][topbar]');
        }

        foreach ($actions as $key => $actionCfg) {
            $isGranted = $this->accessHelper->isActionAllowed($class, $action);
            if ($isGranted && $key != $action) {
                $actionType = $actionCfg['type'] ?? 'default';
                switch ($actionType) {
                    case 'default':
                        $actions[$key]['link'] = $this->adminRouter->generate($entity, $actionCfg['action']);
                        break;
                    case 'forward':
                        $relatedEntityConfig = $this->entityHelper->getEntityConfig($actionCfg['entity']);
                        // maybe use forward route ???
                        $actions[$key]['link'] = $this->adminRouter->generateUrlFromRoute(
                            'aa_admin_entity',
                            [
                                'entitySlug' => $relatedEntityConfig['slug'],
                                'action' => $actionCfg['action'],
                            ]
                        );
                        break;
                }

            } else {
                unset($actions[$key]);
            }
        }

        return $actions;
    }
}
