const express = require('express');

/**
 * Arr routes aggregator
 * Assembles all arr 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 = {
    docker: ctx.docker,
    credentialManager: ctx.credentialManager,
    servicesStateManager: ctx.servicesStateManager,
    fetchT: ctx.fetchT,
    asyncHandler: ctx.asyncHandler,
    errorResponse: ctx.errorResponse,
    log: ctx.log,
    // Additional context properties needed by arr routes
    notification: ctx.notification,
    safeErrorMessage: ctx.safeErrorMessage
  };

  // Initialize helpers with dependencies
  const helpers = require('./helpers')(ctx);

  // Mount sub-routes — pass full ctx so sub-routes can reference ctx.* properties
  const subCtx = Object.assign({}, ctx, { helpers });
  router.use(require('./detect')(subCtx));
  router.use(require('./credentials')(subCtx));
  router.use(require('./config')(subCtx));
  router.use(require('./smart-connect')(subCtx));
  router.use(require('./plex')(subCtx));

  return router;
};
