<?php

declare(strict_types=1);

namespace Apexl\Io\BasicModule\BasicDisplay;

use Apexl\Config\ConfigurationInterface;
use Apexl\Io\BasicModule\BasicApi\Services\BasicModuleEntityStore;
use Apexl\Io\Menu\Interfaces\ProvidesMenuItemsInterface;
use Apexl\Io\Menu\Services\MenuManager;
use Apexl\Io\Module;
use Slim\App;

/**
 * A base for display modules that keep a list of entities and put them in the menu.
 *
 * It used to declare six routes per entity: five against
 * `DisplayController::all|view|create|delete`, whose only public method is __construct, and
 * one against a `formsController` that exists in no package. Every one of them answered 500
 * for every caller. Removed rather than reimplemented — there is nothing behind them to
 * point at, and rebuilding them would only re-register the same failures.
 *
 * The menu entries stay; those never went through the route builder.
 */
readonly class BasicDisplayModule extends Module implements ProvidesMenuItemsInterface
{
    public function __construct(
        protected BasicModuleEntityStore $moduleStore,
        ConfigurationInterface $config,
        App $app,
    ) {
        parent::__construct($config, $app);
    }

    protected function moduleStoreKey(): string
    {
        return $this::class;
    }

    public function menuItems(MenuManager $menuManager): void
    {
        if ($entities = $this->moduleStore->all($this->moduleStoreKey())) {
            foreach ($entities as $entityClass) {
                $menuManager->addToMenu($entityClass::entityName() . '.adminParent', [
                    'title' => $entityClass::niceName(),
                ], 'administration');
                $menuManager->addToMenu($entityClass::entityName() . '.display.all', [
                    'title' => 'View List',
                    'parent' => $entityClass::entityName() . '.adminParent',
                ], 'administration');
                $menuManager->addToMenu($entityClass::entityName() . '.display.post', [
                    'title' => 'Create',
                    'parent' => $entityClass::entityName() . '.adminParent',
                ], 'administration');
            }
        }
    }
}
