<?php

namespace AutoAdmin\Bundle\Admin\Helper;

use AutoAdmin\Bundle\Admin\Configuration\ConfigManager;
use AutoAdmin\Bundle\Admin\Exception\UnknownEntityException;
use AutoAdmin\Bundle\Core\Helper\DoctrineHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Serializer\SerializerInterface;

class EntityHelper
{
    public function __construct(
        private ConfigManager $configManager,
        private EntityManagerInterface $entityManager,
        private SerializerInterface $serializer,
    ) {
    }

    public function findFullClassName(string $entitySlug): int|string|null
    {
        // if slug is a namespace and contains backslashes, return as is
        if (stripos($entitySlug, '\\') !== false) {
            return $entitySlug;
        }

        $entitySlug = strtolower($entitySlug);

        foreach ($this->configManager->getConfig()['entities'] as $fullClassName => $config) {
            if (!empty($config['slug']) && $config['slug'] === $entitySlug) {
                return $fullClassName;
            }
            if ($entitySlug === self::extractShortEntityName($fullClassName)) {
                return $fullClassName;
            }
        }

        return null;
    }

    public static function extractShortEntityName(string $className): string
    {
        $elements = explode('\\', $className);

        return strtolower(end($elements));
    }

    public function getEntityConfig($className)
    {
        return $this->configManager->getEntityConfigByClass($className);
    }

    public function getEntitySlugFromClass($fqdn)
    {
        $config = $this->configManager->getEntityConfigByClass($fqdn);

        return $config['slug'];
    }

    public function getEntityClassFromSlug($slug)
    {
        $config = $this->configManager->getEntityConfigBySlug($slug);

        return $config['class'];
    }

    public function getManager(): EntityManagerInterface
    {
        return $this->entityManager;
    }

    public function getSerializer(): SerializerInterface
    {
        return $this->serializer;
    }

    public function getEntityNameFromSlug($slug)
    {
        $config = $this->configManager->getEntityConfigBySlug($slug);

        return $config['name'];
    }

    public function getEntityNameFromFqdn($fqdn)
    {
        $config = $this->configManager->getEntityConfigByClass($fqdn);

        return $config['name'];
    }

    public function guessFqcn($entity)
    {
        if (is_object($entity)) {
            $class = get_class($entity);
            $class = DoctrineHelper::removeProxyPrefix($class);
            if ($metadata = $this->entityManager->getClassMetadata($class)) {
                $class = $metadata->getName();
            }
        } elseif (is_string($entity) && stripos($entity, '\\') === false) {
            $class = $this->getEntityClassFromSlug($entity);
        } elseif (is_string($entity) && class_exists($entity)) {
            $class = $entity;
        } else {
            throw new UnknownEntityException($entity);
        }

        return $class;
    }
}
