<?php

declare(strict_types=1);

namespace apexl\Io\modules\userDisplay\hooks;

use apexl\Io\includes\RouteManager;
use apexl\Io\interfaces\AbstractHook;
use apexl\Io\interfaces\OutputManagerInterface;

final readonly class RegisterRedirects implements AbstractHook
{

    public function __construct(private RouteManager $routeManager) {}

    public function hooks(): array
    {
        return [
            OutputManagerInterface::HOOK__ADD_RESPONSE,
        ];
    }

    public function __invoke(array $response, string $route, OutputManagerInterface $output): void
    {
        switch ($route) {
            case 'user.login':
                if ($response['success']) {
                    $output->addMetadata(
                        'user.login.redirect',
                        'redirect',
                        $this->routeManager->getRoutePattern('home')
                    );
                }
                break;
            case 'user.logout':
                if ($response['success']) {
                    $output->addMetadata(
                        'user.logout.redirect',
                        'redirect',
                        $this->routeManager->getRoutePattern('home')
                    );
                }
                break;
            case 'userEntity.post':
                // Was: redirect to the now-removed 'userEntity.display.get'
                // (userViewSingle) page. That route rendered deprecated
                // io-display builders and has been removed; redirect home
                // instead (apexl/io-user-tabulator supplies the real
                // replacement listing/view in consuming applications).
                if ($response['success']) {
                    $output->addMetadata(
                        'userEntity.post.redirect',
                        'redirect',
                        $this->routeManager->getRoutePattern('home')
                    );
                }
                break;
            case 'userEntity.put':
                // Was: redirect to the now-removed 'userEntity.display.get'
                // page; see the 'userEntity.post' case above.
                if ($response['success']) {
                    $output->addMetadata(
                        'userEntity.put.redirect',
                        'redirect',
                        $this->routeManager->getRoutePattern('home')
                    );
                }
                break;
            case 'user.forgot-password.hash':
                // Was: redirect to the now-removed 'user.display.settings'
                // page (dead vendor code; missing Form/InputField classes).
                if ($response['success']) {
                    $output->addMetadata(
                        'userEntity.post.redirect',
                        'redirect',
                        $this->routeManager->getRoutePattern('home')
                    );
                }
                break;
            case 'roleEntity.post':
            case 'roleEntity.put':
            case 'roleEntity.delete':
                // Was: redirect to the now-removed 'roleEntity.display.all'
                // (roleList) page, which rendered a deprecated io-display
                // builder and has been removed along with the rest of the
                // roleEntity display route bundle. Redirect home instead.
                $output->addMetadata(
                    $route . '.redirect',
                    'redirect',
                    $this->routeManager->getRoutePattern('home')
                );
                break;
        }
    }
}
