<?php
namespace Misuzu;

use Index\Routing\Route;
use Index\Routing\RouteHandler;
use Misuzu\URLs\IURLSource;
use Misuzu\URLs\URLInfo;
use Misuzu\URLs\URLRegistry;

class LegacyRoutes extends RouteHandler implements IURLSource {
    public function __construct(
        private URLRegistry $urls
    ) {}

    public function registerURLs(URLRegistry $urls): void {
        // eventually this should be handled by context classes
        $urls->register('search-index', '/search.php');
        $urls->register('search-query', '/search.php', ['q' => '<query>'], '<section>');

        $urls->register('auth-login',         '/auth/login.php',     ['username' => '<username>', 'redirect' => '<redirect>']);
        $urls->register('auth-login-welcome', '/auth/login.php',     ['welcome' => '1', 'username' => '<username>']);
        $urls->register('auth-register',      '/auth/register.php');
        $urls->register('auth-forgot',        '/auth/password.php');
        $urls->register('auth-reset',         '/auth/password.php',  ['user' => '<user>']);
        $urls->register('auth-logout',        '/auth/logout.php',    ['csrf' => '<csrf>']);
        $urls->register('auth-resolve-user',  '/auth/login.php',     ['resolve' => '1', 'name' => '<username>']);
        $urls->register('auth-two-factor',    '/auth/twofactor.php', ['token' => '<token>']);
        $urls->register('auth-revert',        '/auth/revert.php',    ['csrf' => '<csrf>']);

        $urls->register('forum-index',         '/forum');
        $urls->register('forum-leaderboard',   '/forum/leaderboard.php', ['id' => '<id>', 'mode' => '<mode>']);
        $urls->register('forum-mark-global',   '/forum/index.php',       ['m' => 'mark']);
        $urls->register('forum-mark-single',   '/forum/index.php',       ['m' => 'mark', 'f' => '<forum>']);
        $urls->register('forum-topic-new',     '/forum/posting.php',     ['f' => '<forum>']);
        $urls->register('forum-reply-new',     '/forum/posting.php',     ['t' => '<topic>']);
        $urls->register('forum-category',      '/forum/forum.php',       ['f' => '<forum>', 'p' => '<page>']);
        $urls->register('forum-category-root', '/forum/index.php',       fragment: '<forum>');
        $urls->register('forum-topic',         '/forum/topic.php',       ['t' => '<topic>', 'page' => '<page>']);
        $urls->register('forum-topic-create',  '/forum/posting.php',     ['f' => '<forum>']);
        $urls->register('forum-topic-bump',    '/forum/topic.php',       ['t' => '<topic>', 'm' => 'bump',    'csrf' => '<csrf>']);
        $urls->register('forum-topic-lock',    '/forum/topic.php',       ['t' => '<topic>', 'm' => 'lock',    'csrf' => '<csrf>']);
        $urls->register('forum-topic-unlock',  '/forum/topic.php',       ['t' => '<topic>', 'm' => 'unlock',  'csrf' => '<csrf>']);
        $urls->register('forum-topic-delete',  '/forum/topic.php',       ['t' => '<topic>', 'm' => 'delete',  'csrf' => '<csrf>']);
        $urls->register('forum-topic-restore', '/forum/topic.php',       ['t' => '<topic>', 'm' => 'restore', 'csrf' => '<csrf>']);
        $urls->register('forum-topic-nuke',    '/forum/topic.php',       ['t' => '<topic>', 'm' => 'nuke',    'csrf' => '<csrf>']);
        $urls->register('forum-post',          '/forum/topic.php',       ['p' => '<post>'], 'p<post>');
        $urls->register('forum-post-create',   '/forum/posting.php',     ['t' => '<topic>']);
        $urls->register('forum-post-delete',   '/forum/post.php',        ['p' => '<post>', 'm' => 'delete']);
        $urls->register('forum-post-restore',  '/forum/post.php',        ['p' => '<post>', 'm' => 'restore']);
        $urls->register('forum-post-nuke',     '/forum/post.php',        ['p' => '<post>', 'm' => 'nuke']);
        $urls->register('forum-post-quote',    '/forum/posting.php',     ['q' => '<post>']);
        $urls->register('forum-post-edit',     '/forum/posting.php',     ['p' => '<post>', 'm' => 'edit']);

        $urls->register('user-list', '/members.php', ['r' => '<role>', 'ss' => '<sort>', 'sd' => '<direction>', 'p' => '<page>']);

        $urls->register('user-profile',              '/profile.php', ['u' => '<user>']);
        $urls->register('user-profile-forum-topics', '/profile.php', ['u' => '<user>', 'm' => 'forum-topics']);
        $urls->register('user-profile-forum-posts',  '/profile.php', ['u' => '<user>', 'm' => 'forum-posts']);
        $urls->register('user-profile-edit',         '/profile.php', ['u' => '<user>', 'edit' => '1']);
        $urls->register('user-account-standing',     '/profile.php', ['u' => '<user>'], 'account-standing');

        $urls->register('settings-index',        '/settings');
        $urls->register('settings-account',      '/settings/account.php');
        $urls->register('settings-sessions',     '/settings/sessions.php', ['p' => '<page>']);
        $urls->register('settings-logs',         '/settings/logs.php');
        $urls->register('settings-logs-logins',  '/settings/logs.php', ['ap' => '<account-page>', 'hp' => '<page>'], 'login-history');
        $urls->register('settings-logs-account', '/settings/logs.php', ['hp' => '<logins-page>', 'ap' => '<page>'], 'account-log');
        $urls->register('settings-data',         '/settings/data.php');

        $urls->register('comment-create',  '/comments.php', ['m' => 'create', 'return' => '<return>']);
        $urls->register('comment-vote',    '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'vote', 'v' => '<vote>', 'return' => '<return>']);
        $urls->register('comment-delete',  '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'delete', 'return' => '<return>']);
        $urls->register('comment-restore', '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'restore', 'return' => '<return>']);
        $urls->register('comment-pin',     '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'pin', 'return' => '<return>']);
        $urls->register('comment-unpin',   '/comments.php', ['c' => '<comment>', 'csrf' => '<csrf>', 'm' => 'unpin', 'return' => '<return>']);

        $urls->register('manage-general-overview', '/manage/general');
        $urls->register('manage-general-logs',     '/manage/general/logs.php', ['p' => '<page>']);

        $urls->register('manage-general-emoticons',           '/manage/general/emoticons.php');
        $urls->register('manage-general-emoticon',            '/manage/general/emoticon.php',  ['e' => '<emote>']);
        $urls->register('manage-general-emoticon-order-up',   '/manage/general/emoticons.php', ['emote' => '<emote>', 'order' => 'd', 'csrf' => '<csrf>']);
        $urls->register('manage-general-emoticon-order-down', '/manage/general/emoticons.php', ['emote' => '<emote>', 'order' => 'i', 'csrf' => '<csrf>']);
        $urls->register('manage-general-emoticon-delete',     '/manage/general/emoticons.php', ['emote' => '<emote>', 'delete' => '1', 'csrf' => '<csrf>']);
        $urls->register('manage-general-emoticon-alias',      '/manage/general/emoticons.php', ['emote' => '<emote>', 'alias' => '<string>', 'csrf' => '<csrf>']);

        $urls->register('manage-general-settings',       '/manage/general/settings.php');
        $urls->register('manage-general-setting',        '/manage/general/setting.php',        ['name' => '<name>', 'type' => '<type>']);
        $urls->register('manage-general-setting-delete', '/manage/general/setting-delete.php', ['name' => '<name>']);

        $urls->register('manage-forum-categories',          '/manage/forum/index.php');
        $urls->register('manage-forum-category',            '/manage/forum/category.php', ['f' => '<forum>']);
        $urls->register('manage-forum-topic-redirs',        '/manage/forum/redirs.php',   ['p' => '<page>']);
        $urls->register('manage-forum-topic-redirs-create', '/manage/forum/redirs.php');
        $urls->register('manage-forum-topic-redirs-nuke',   '/manage/forum/redirs.php',   ['m' => 'explode', 't' => '<topic>', 'csrf' => '<csrf>']);

        $urls->register('manage-changelog-changes',       '/manage/changelog',            ['p' => '<page>']);
        $urls->register('manage-changelog-change',        '/manage/changelog/change.php', ['c' => '<change>']);
        $urls->register('manage-changelog-change-delete', '/manage/changelog/change.php', ['c' => '<change>', 'delete' => '1', 'csrf' => '<csrf>']);
        $urls->register('manage-changelog-tags',          '/manage/changelog/tags.php');
        $urls->register('manage-changelog-tag',           '/manage/changelog/tag.php',    ['t' => '<tag>']);
        $urls->register('manage-changelog-tag-delete',    '/manage/changelog/tag.php',    ['t' => '<tag>', 'delete' => '1', 'csrf' => '<csrf>']);

        $urls->register('manage-news-categories',      '/manage/news/categories.php', ['p' => '<page>']);
        $urls->register('manage-news-category',        '/manage/news/category.php',   ['c' => '<category>']);
        $urls->register('manage-news-category-delete', '/manage/news/category.php',   ['c' => '<category>', 'delete' => '1', 'csrf' => '<csrf>']);
        $urls->register('manage-news-posts',           '/manage/news/posts.php',      ['p' => '<page>']);
        $urls->register('manage-news-post',            '/manage/news/post.php',       ['p' => '<post>']);
        $urls->register('manage-news-post-delete',     '/manage/news/post.php',       ['p' => '<post>', 'delete' => '1', 'csrf' => '<csrf>']);

        $urls->register('manage-users',                '/manage/users',              ['p' => '<page>']);
        $urls->register('manage-user',                 '/manage/users/user.php',     ['u' => '<user>']);
        $urls->register('manage-users-warnings',       '/manage/users/warnings.php', ['u' => '<user>', 'p' => '<page>']);
        $urls->register('manage-users-warning',        '/manage/users/warning.php',  ['u' => '<user>']);
        $urls->register('manage-users-warning-delete', '/manage/users/warning.php',  ['w' => '<warning>', 'delete' => '1', 'csrf' => '<csrf>']);
        $urls->register('manage-users-notes',          '/manage/users/notes.php',    ['u' => '<user>', 'p' => '<page>']);
        $urls->register('manage-users-note',           '/manage/users/note.php',     ['n' => '<note>', 'u' => '<user>']);
        $urls->register('manage-users-note-delete',    '/manage/users/note.php',     ['n' => '<note>', 'delete' => '1', 'csrf' => '<csrf>']);
        $urls->register('manage-users-bans',           '/manage/users/bans.php',     ['u' => '<user>', 'p' => '<page>']);
        $urls->register('manage-users-ban',            '/manage/users/ban.php',      ['u' => '<user>']);
        $urls->register('manage-users-ban-delete',     '/manage/users/ban.php',      ['b' => '<ban>', 'delete' => '1', 'csrf' => '<csrf>']);

        $urls->register('manage-roles', '/manage/users/roles.php', ['p' => '<page>']);
        $urls->register('manage-role',  '/manage/users/role.php',  ['r' => '<role>']);
    }

    #[Route('GET', '/index.php')]
    public function getIndexPHP($response): void {
        $response->redirect($this->urls->format('index'), true);
    }

    #[Route('GET', '/info.php')]
    public function getInfoPHP($response): void {
        $response->redirect($this->urls->format('info'), true);
    }

    #[Route('GET', '/info.php/:name')]
    public function getInfoDocsPHP($response, $request, string $name): void {
        $response->redirect($this->urls->format('info', ['title' => $name]), true);
    }

    #[Route('GET', '/info.php/:project/:name')]
    public function getInfoProjectPHP($response, $request, string $project, string $name): void {
        $response->redirect($this->urls->format('info', ['title' => $project . '/' . $name]), true);
    }

    #[Route('GET', '/news.php')]
    public function getNewsPHP($response, $request): void {
        $postId = (int)($request->getParam('n', FILTER_SANITIZE_NUMBER_INT) ?? $request->getParam('p', FILTER_SANITIZE_NUMBER_INT));

        if($postId > 0)
            $location = $this->urls->format('news-post', ['post' => $postId]);
        else {
            $catId = (int)$request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
            $pageId = $request->getParam('page', FILTER_SANITIZE_NUMBER_INT);
            $location = $this->urls->format($catId > 0 ? 'news-category' : 'news-index', ['category' => $catId, 'page' => $pageId]);
        }

        $response->redirect($location, true);
    }

    #[Route('GET', '/news.php/rss')]
    public function getNewsRssPHP($response, $request): void {
        $catId = (int)$request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
        $location = $this->urls->format($catId > 0 ? 'news-category-feed-rss' : 'news-feed-rss', ['category' => $catId]);
        $response->redirect($location, true);
    }

    #[Route('GET', '/news.php/atom')]
    public function getNewsAtomPHP($response, $request): void {
        $catId = (int)$request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
        $location = $this->urls->format($catId > 0 ? 'news-category-feed-atom' : 'news-feed-atom', ['category' => $catId]);
        $response->redirect($location, true);
    }

    #[Route('GET', '/news/index.php')]
    public function getNewsIndexPHP($response, $request): void {
        $response->redirect($this->urls->format('news-index', [
            'page' => $request->getParam('page', FILTER_SANITIZE_NUMBER_INT),
        ]), true);
    }

    #[Route('GET', '/news/category.php')]
    public function getNewsCategoryPHP($response, $request): void {
        $response->redirect($this->urls->format('news-category', [
            'category' => $request->getParam('c', FILTER_SANITIZE_NUMBER_INT),
            'page' => $request->getParam('p', FILTER_SANITIZE_NUMBER_INT),
        ]), true);
    }

    #[Route('GET', '/news/post.php')]
    public function getNewsPostPHP($response, $request): void {
        $response->redirect($this->urls->format('news-post', [
            'post' => $request->getParam('p', FILTER_SANITIZE_NUMBER_INT),
        ]), true);
    }

    #[Route('GET', '/news/feed.php')]
    public function getNewsFeedPHP($response, $request): int {
        return 400;
    }

    #[Route('GET', '/news/feed.php/rss')]
    public function getNewsFeedRssPHP($response, $request): void {
        $catId = (int)$request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
        $response->redirect($this->urls->format(
            $catId > 0 ? 'news-category-feed-rss' : 'news-feed-rss',
            ['category' => $catId]
        ), true);
    }

    #[Route('GET', '/news/feed.php/atom')]
    public function getNewsFeedAtomPHP($response, $request): void {
        $catId = (int)$request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
        $response->redirect($this->urls->format(
            $catId > 0 ? 'news-category-feed-atom' : 'news-feed-atom',
            ['category' => $catId]
        ), true);
    }

    #[Route('GET', '/changelog.php')]
    public function getChangelogPHP($response, $request): void {
        $changeId = $request->getParam('c', FILTER_SANITIZE_NUMBER_INT);
        if($changeId) {
            $response->redirect($this->urls->format('changelog-change', ['change' => $changeId]), true);
            return;
        }

        $response->redirect($this->urls->format('changelog-index', [
            'date' => $request->getParam('d'),
            'user' => $request->getParam('u', FILTER_SANITIZE_NUMBER_INT),
        ]), true);
    }

    #[Route('GET', '/auth.php')]
    public function getAuthPHP($response, $request): void {
        $response->redirect($this->urls->format(match($request->getParam('m')) {
            'logout' => 'auth-logout',
            'reset' => 'auth-reset',
            'forgot' => 'auth-forgot',
            'register' => 'auth-register',
            default => 'auth-login',
        }), true);
    }

    #[Route('GET', '/auth')]
    #[Route('GET', '/auth/index.php')]
    public function getAuthIndexPHP($response, $request): void {
        $response->redirect($this->urls->format('auth-login'), true);
    }

    #[Route('GET', '/settings.php')]
    public function getSettingsPHP($response): void {
        $response->redirect($this->urls->format('settings-index'), true);
    }

    #[Route('GET', '/settings')]
    public function getSettingsIndex($response): void {
        $response->redirect($this->urls->format('settings-account'));
    }

    #[Route('GET', '/settings/index.php')]
    public function getSettingsIndexPHP($response): void {
        $response->redirect($this->urls->format('settings-account'), true);
    }

    #[Route('GET', '/manage')]
    #[URLInfo('manage-index', '/manage')]
    public function getManageIndex($response): void {
        $response->redirect($this->urls->format('manage-general-overview'));
    }

    #[Route('GET', '/manage/index.php')]
    public function getManageIndexPHP($response): void {
        $response->redirect($this->urls->format('manage-general-overview'), true);
    }

    #[Route('GET', '/manage/news')]
    public function getManageNewsIndex($response): void {
        $response->redirect($this->urls->format('manage-news-categories'));
    }

    #[Route('GET', '/manage/news/index.php')]
    public function getManageNewsIndexPHP($response): void {
        $response->redirect($this->urls->format('manage-news-categories'), true);
    }
}
