<?php

namespace apexl\Io\modules\BasicDisplay;

use apexl\Config\ConfigurationInterface;
use apexl\Io\includes\RouteManager;
use apexl\Io\modules\BasicApi\Services\BasicModuleEntityStore;
use apexl\Io\modules\display\controllers\DisplayController;
use apexl\Io\modules\display\controllers\formsController;
use apexl\Io\modules\menu\interfaces\ProvidesMenuItemsInterface;
use apexl\Io\modules\menu\services\MenuManager;
use apexl\Io\modules\system\interfaces\ProvidesRoutesInterface;
use apexl\Io\modules\user\classes\userManagedDisplayModule;
use apexl\Io\modules\user\enums\permissions\Common;
use apexl\Io\services\InstallChecker;

/**
 * Base module file, provides required methods to register things like routes and middleware.
 * Class installModule
 * @package apexl\Io\modules\install
 */
class BasicDisplayModule extends userManagedDisplayModule implements ProvidesMenuItemsInterface, ProvidesRoutesInterface
{
    public function __construct(
        protected BasicModuleEntityStore $moduleStore,
        ConfigurationInterface $config,
        InstallChecker $installChecker,
    ) {
        parent::__construct($installChecker, $config);
    }

    /**
     * @Todo - Refine permissions. For now, using Core ENUMs
     */
    public function routes(RouteManager $routeManager): void
    {
        if ($entities = $this->moduleStore->all($this->moduleStoreKey())) {
            foreach ($entities as $entityClass) {
                $this->addProtectedEntityRoutes($routeManager, $entityClass, [
                    'all' => [DisplayController::class . ':all', Common::VIEW],
                    'get' => [DisplayController::class . ':view', Common::VIEW],
                    'put' => [DisplayController::class . ':create', Common::UPDATE],
                    'post' => [DisplayController::class . ':create', Common::CREATE],
                    'delete' => [DisplayController::class . ':delete', Common::DELETE],
                ]);

                //add Form Route
                $this->addProtectedEntityFormRoute(
                    $routeManager,
                    $entityClass,
                    $entityClass::entityName() . '/createUpdate[/{id}]',
                    formsController::class . ':entityCreateUpdate',
                );
            }
        }
    }

    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');
            }
        }
    }
}
