<?php

/** @noinspection PhpUnused */

namespace apexl\Io\modules\userDisplay;

use apexl\Io\enums\HttpMethod;
use apexl\Io\includes\RouteManager;
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\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\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.

        // Removed: the last three userEntity display routes — /user/create
        // ('post'), /user/{id}/edit ('put') and /user/{id}/delete ('delete').
        // The first two rendered userController::create()/updateView(), which
        // both build their form through userEditForm() — that references Form
        // and InputField classes deleted in the V3 restructure, so both routes
        // were a guaranteed 500. The third pointed at DisplayController::delete(),
        // a method that does not exist. Together with 'all'/'get' (removed
        // earlier for the same class of reason) that retires the whole vendor
        // user-admin surface; it is superseded by apexl/io-user-tabulator plus
        // whatever admin API the consuming application registers.

        // 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.

        // Removed: 'userEntity.data.summery' (user-summery). userSummery()
        // ended in a call to getEntityData(), which does not exist on
        // Controller — the endpoint could never return.
        // Removed: 'roleEntity.data.summery' (role-summery) was only consumed
        // by roleEntityDisplayTile/rolesController::roleSummery, both removed.

        // Removed: the whole legacy /forms/* bundle — login, forgottenPassword,
        // register, userSettings, createUpdate, loginAsUser, createNote and the
        // userFilter filter form. Every one of them was a 500 for every caller:
        //   - login / forgottenPassword / userFilter built their payload from
        //     apexl\Io\modules\formbuilder\entities\formEntity and inputField,
        //     both deleted in the V3 restructure (and their form classes read
        //     $this->routeManager, which was never assigned either);
        //   - register / createUpdate route into userEditForm() and
        //     primaryAccountForm(), which construct an unimported Form and
        //     InputField;
        //   - userSettingsForm(), loginAsUserForm() and the entire
        //     userNoteController class do not exist.
        // These are the V2 "server renders the form definition" endpoints. V3
        // builds forms through the FormBuilders/ classes that the live page
        // controllers (LoginDisplayController, ForgotPasswordDisplayController,
        // RegisterDisplayController, ResetPasswordDisplayController) already
        // use, so the password-recovery and sign-in flows are unaffected.

        $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.
        // Removed: 'userEntity.display.post' (Create a user) menu item pointed
        // at the removed /user/create route — the one dead route in this set
        // that was actually linked from the UI, so an admin who opened it got
        // a 500 rather than a form.
        // apexl/io-user-tabulator registers its own equivalent menu items in
        // consuming applications.
    }

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