/**
 * Example Routes
 * Demonstrates routing patterns
 */
import { AuthController } from '../app/Controllers/AuthController.js';
import { UserQueryBuilderController } from '../app/Controllers/UserQueryBuilderController.js';
import { Env } from '../src/config/env.js';
import { Router } from '../src/routes/Router.js';
import { registerBroadcastRoutes } from './broadcast.js';
import { registerStorageRoutes } from './storage.js';
import { getRuntimeMode } from '../src/runtime/detectRuntime.js';
import { ErrorFactory } from '../src/index.js';
export function registerRoutes(router) {
    try {
        const authController = AuthController.create();
        const userController = UserQueryBuilderController.create();
        registerPublicRoutes(router);
        registerApiV1Routes(router, authController, userController);
        registerAdminRoutes(router);
    }
    catch (error) {
        throw ErrorFactory.createConfigError(`Failed to register routes: ${error.message}`, error);
    }
}
/**
 * Register public routes
 */
function registerPublicRoutes(router) {
    registerRootRoute(router);
    registerHealthRoute(router);
    registerBroadcastRoutes(router);
    registerStorageRoutes(router);
}
function registerHealthRoute(router) {
    Router.get(router, '/health', async (_req, res) => {
        const modeFromEnv = Env.get('RUNTIME_MODE', '').trim();
        const mode = modeFromEnv === '' ? getRuntimeMode() : modeFromEnv;
        res.json({
            status: 'ok',
            mode,
            worker: Env.get('WORKER_ENABLED', 'false') === 'true',
            timestamp: new Date().toISOString(),
        });
    });
}
function registerRootRoute(router) {
    Router.get(router, '/', async (_req, res) => {
        res.json({
            framework: 'ZinTrust Framework',
            app_name: Env.APP_NAME,
            version: '0.1.41',
            env: Env.NODE_ENV ?? 'development',
            database: Env.DB_CONNECTION ?? 'sqlite',
        });
    });
}
/**
 * Register API V1 routes
 */
function registerApiV1Routes(router, authController, userController) {
    Router.group(router, '/api/v1', (r) => {
        // Auth routes
        Router.post(r, '/auth/login', authController.login, {
            middleware: ['authRateLimit', 'validateLogin'],
        });
        Router.post(r, '/auth/register', authController.register, {
            middleware: ['authRateLimit', 'validateRegister'],
        });
        Router.post(r, '/auth/logout', authController.logout, {
            middleware: ['auth', 'jwt'],
        });
        Router.post(r, '/auth/logout-all', authController.logoutAll, {
            middleware: ['auth', 'jwt'],
        });
        Router.post(r, '/auth/refresh', authController.refresh, {
            middleware: ['auth', 'jwt'],
        });
        // Protected routes (Router supports per-route middleware metadata)
        const pr = r;
        // User resource (REST-ish)
        Router.resource(pr, '/users', {
            index: userController.index,
            store: userController.store,
            show: userController.show,
            update: userController.update,
            destroy: userController.destroy,
        }, {
            middleware: ['auth', 'bulletproof'],
            store: {
                middleware: ['auth', 'bulletproof', 'userMutationRateLimit', 'validateUserStore'],
            },
            update: {
                middleware: ['auth', 'bulletproof', 'userMutationRateLimit', 'validateUserUpdate'],
            },
            destroy: { middleware: ['auth', 'bulletproof', 'userMutationRateLimit'] },
        });
        Router.post(pr, '/users/fill', userController.fill, {
            middleware: ['auth', 'jwt', 'fillRateLimit', 'validateUserFill'],
        });
        // If the controller exposes create/edit, wire them explicitly.
        Router.get(pr, '/users/create', userController.create, {
            middleware: ['auth', 'jwt'],
        });
        Router.get(pr, '/users/:id/edit', userController.edit, {
            middleware: ['auth', 'jwt'],
        });
        // Custom user routes
        Router.get(pr, '/profile', async (__req, res) => {
            res.json({ message: 'Get user profile' });
        }, { middleware: ['auth', 'bulletproof'] });
        Router.put(pr, '/profile', async (__req, res) => {
            res.json({ message: 'Update user profile' });
        }, { middleware: ['auth', 'bulletproof'] });
        // Posts resource
        Router.get(r, '/posts', async (_req, res) => {
            res.json({ data: [] });
        });
        Router.get(r, '/posts/:id', async (req, res) => {
            const id = req.getParam('id');
            res.json({ data: { id } });
        });
    });
}
/**
 * Register admin routes
 */
function registerAdminRoutes(router) {
    Router.group(router, '/admin', (r) => {
        Router.get(r, '/dashboard', async (__req, res) => {
            res.json({ message: 'Admin dashboard' });
        });
        Router.get(r, '/users', async (__req, res) => {
            res.json({ data: [] });
        });
    });
}
