<?php

/** @noinspection PhpUnused */

namespace apexl\Io\modules\userDisplay;

use apexl\Io\enums\HttpMethod;
use apexl\Io\includes\RouteManager;
use apexl\Io\modules\display\Controllers\DisplayController;
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\entities\userEntity;
use apexl\Io\modules\user\enums\permissions\Common;
use apexl\Io\modules\user\enums\permissions\User;
use apexl\Io\modules\userDisplay\controllers\ForgotPasswordCheckEmailDisplayController;
use apexl\Io\modules\userDisplay\controllers\ForgotPasswordDisplayController;
use apexl\Io\modules\userDisplay\controllers\LoginDisplayController;
use apexl\Io\modules\userDisplay\controllers\LoginWithNfcDisplayController;
use apexl\Io\modules\userDisplay\controllers\RegisterDisplayController;
use apexl\Io\modules\userDisplay\controllers\ResetPasswordDisplayController;
use apexl\Io\modules\userDisplay\controllers\ResetPasswordSuccessDisplayController;
use apexl\Io\modules\userDisplay\controllers\userController;
use apexl\Io\modules\userDisplay\forms\filters;
use apexl\Io\modules\userDisplay\hooks\RegisterRedirects;
use app\vendor\apexl\io\src\Io\interfaces\ProvidesHooksInterface;

class userDisplayModule extends userManagedDisplayModule implements ProvidesHooksInterface, ProvidesMenuItemsInterface, ProvidesRoutesInterface
{
    public function routes(RouteManager $routeManager): void
    {
        //Page routes.
        $this->addProtectedRootRoute(
            $routeManager,
            HttpMethod::GET,
            'user.display.login.nfc',
            'login/nfc',
            LoginWithNfcDisplayController::class,
        );
        $this->addProtectedRootRoute(
            $routeManager,
            HttpMethod::GET,
            'user.display.login',
            'login[/{loginMessageType}]',
            LoginDisplayController::class
        );
        $this->addProtectedRootRoute(
            $routeManager,
            HttpMethod::GET,
            'user.display.register',
            'register',
            RegisterDisplayController::class,
        );

        $this->addProtectedRootRoute(
            $routeManager,
            HttpMethod::GET,
            'user.display.forgot-password.check-email',
            'forgot-password/check-email',
            ForgotPasswordCheckEmailDisplayController::class
        );

        $this->addProtectedRootRoute(
            $routeManager,
            HttpMethod::GET,
            'user.display.forgot-password',
            'forgot-password',
            ForgotPasswordDisplayController::class
        );

        // Removed: vendor /users + /user/settings root routes pointed at
        // userHome() / userSettingsPage(), both dead vendor code (userHome()
        // rendered userEntityDisplayTile/roleEntityDisplayTile, which extend
        // the deprecated io-display EntityDisplayTile builder and are fatal
        // under the readonly Component contract; userSettingsPage() called
        // userEditForm(), which references Form/InputField classes that do
        // not exist in this package and is fatal for an unrelated reason).
        // Superseded by apexl/io-user-tabulator in consuming applications.

        //These are display routes, so in this instance VERBS are used to dictate the pattern, NOT the HTTP verb.
        // 'all' (userList) and 'get' (userViewSingle) removed: both render
        // deprecated io-display builders (CardEntityFilteredDisplayTable /
        // EntityDisplayTable, and DisplayTable + ButtonBar respectively) that
        // are fatal under the readonly Component contract. Superseded by
        // apexl/io-user-tabulator. 'put'/'post'/'delete' are left registered.
        $this->addProtectedEntityRoutes(
            $routeManager,
            userEntity::class,
            [
                'put' => [userController::class . '::updateView', User::UPDATE],
                'post' => [userController::class . '::create', User::CREATE],
                'delete' => [displayController::class . '::delete', User::DELETE],
            ]
        );

        // Removed: the entire roleEntity display route bundle (all/get/put/
        // post/delete via rolesController) rendered deprecated io-display
        // builders (CardEntityFilteredDisplayTable/EntityDisplayTable via
        // roleList; the view/put/post render path also depends on the same
        // broken Component construction). Superseded by apexl/io-user-tabulator.

        //data routes
        $this->addProtectedDataRoute(
            $routeManager,
            HttpMethod::GET,
            'userEntity.data.summery',
            'user-summery',
            userController::class . '::userSummery',
            User::VIEW
        );
        // Removed: 'roleEntity.data.summery' (role-summery) was only consumed
        // by roleEntityDisplayTile/rolesController::roleSummery, both removed.

        //Forms - Users
        $this->addProtectedFormRoute(
            $routeManager,
            'user.display.forms.login',
            'loginForm[/{redirectToRoute}]',
            userController::class . '::loginForm'
        );
        $this->addProtectedFormRoute(
            $routeManager,
            'user.display.forms.forgottenPassword',
            'forgottenPasswordForm',
            userController::class . '::forgotPasswordForm'
        );
        $this->addProtectedFormRoute(
            $routeManager,
            'user.display.forms.register',
            'registerForm',
            userController::class . '::registerForm'
        );
        $this->addProtectedFormRoute(
            $routeManager,
            'user.display.forms.userSettings',
            'userSettings',
            userController::class . '::userSettingsForm',
            Common::IS_LOGGED_IN
        );
        $this->addProtectedFormRoute(
            $routeManager,
            'user.display.forms.createUpdate',
            'userCreate[/{id}]',
            userController::class . '::userEditForm',
            User::CREATE
        );
        $this->addProtectedFormRoute(
            $routeManager,
            'user.display.forms.loginAsUser',
            'loginAsUserForm/{userId}',
            userController::class . '::loginAsUserForm',
            User::MANAGE
        );
        $this->addProtectedFormRoute(
            $routeManager,
            'user.display.forms.createNote',
            'create-user-note/{userId}',
            userNoteController::class . '::createNoteForm',
            User::UPDATE
        );

        // Removed: 'role.display.forms.createUpdate' (roleCreate[/{id}]) fed
        // roleEntity create/update, which is removed above; rolesController
        // no longer exists.

        //Filter forms
        $this->addProtectedFormRoute(
            $routeManager,
            'user.display.forms.filters.userFilter',
            'user-filter',
            filters::class . '::userFilter',
            User::VIEW
        );

        $this->addProtectedRootRoute(
            $routeManager,
            HttpMethod::GET,
            'user.display.reset-password',
            'reset-password/{hash}',
            ResetPasswordDisplayController::class,
        );

        $this->addProtectedRootRoute(
            $routeManager,
            HttpMethod::GET,
            'user.display.reset-password-success',
            'reset-password-success',
            ResetPasswordSuccessDisplayController::class
        );
    }

    public function menuItems(MenuManager $menuManager): void
    {
        // Navigation Menu
        $menuManager->addToMenu('home', [
            'title' => 'Home',
            'icon' => 'home',
            'weight' => -10,
        ]);

        //Administration menu
        $menuManager->addToMenu('user.admin', [
            'title' => 'Users',
            'icon' => 'users',
            'weight' => 0,
        ], 'administration');

        // Removed: 'user.display.dashboard' (Overview) menu item pointed at
        // the removed users-dashboard route.
        // Removed: 'userEntity.display.all' (View all users) menu item
        // pointed at the removed userList route.
        // Removed: 'roleEntity.display.all' (View all roles) and
        // 'roleEntity.display.post' (Create a role) menu items pointed at
        // the removed roleEntity display routes.
        // apexl/io-user-tabulator registers its own equivalent menu items in
        // consuming applications.

        $menuManager->addToMenu('userEntity.display.post', [
            'title' => 'Create a user',
            'weight' => -8,
            'parent' => 'user.admin',
            'icon' => 'users-medical',
        ], 'administration');
    }

    public function hooks(): array
    {
        return [
            RegisterRedirects::class,
        ];
    }
}
