const express = require('express');
const initTotp = require('./totp');
const initKeys = require('./keys');
const initSessionHandlers = require('./session-handlers');
const initSsoGate = require('./sso-gate');

/**
 * Auth routes aggregator
 * Assembles all auth sub-routes with their dependencies
 * @param {Object} ctx - Application context (for backward compatibility)
 * @returns {express.Router}
 */
module.exports = function(ctx) {
  const router = express.Router();

  // Extract dependencies from context
  const deps = {
    authManager: ctx.authManager,
    credentialManager: ctx.credentialManager,
    totpConfig: ctx.totpConfig,
    saveTotpConfig: ctx.saveTotpConfig,
    session: ctx.session,
    asyncHandler: ctx.asyncHandler,
    errorResponse: ctx.errorResponse,
    log: ctx.log,
    // Additional deps for sso-gate
    fetchT: ctx.fetchT,
    getServiceById: ctx.getServiceById,
    licenseManager: ctx.licenseManager,
    servicesStateManager: ctx.servicesStateManager,
    renewCSRFToken: ctx.middlewareResult?.renewCSRFToken
  };

  const { getAppSession, appSessionCache } = initSessionHandlers(deps);

  router.use(initTotp(deps));
  router.use(initKeys(deps));
  router.use(initSsoGate({ ...deps, getAppSession, appSessionCache }));

  return router;
};
