/**
 * Context assembly - Dependency injection container
 * Assembles all context objects needed by routes
 */
const { createDockerContext } = require('./docker');
const { createCaddyContext } = require('./caddy');
const { createDnsContext } = require('./dns');
const { createSessionContext } = require('./session');
const NotificationManager = require('../managers/notification-manager');

/**
 * Assemble the full application context
 * This replaces the old "god object" ctx with explicit construction
 */
function assembleContext({
  // Config
  siteConfig,
  buildDomain,
  buildServiceUrl,
  SERVICES_FILE,
  CONFIG_FILE,
  TOTP_CONFIG_FILE,
  TAILSCALE_CONFIG_FILE,
  NOTIFICATIONS_FILE,
  ERROR_LOG_FILE,
  DNS_CREDENTIALS_FILE,
  CADDYFILE_PATH,
  CADDY_ADMIN_URL,
  
  // State managers
  servicesStateManager,
  configStateManager,
  
  // Managers
  credentialManager,
  authManager,
  licenseManager,
  healthChecker,
  updateManager,
  backupManager,
  resourceMonitor,
  auditLogger,
  portLockManager,
  selfUpdater,
  dockerMaintenance,
  logDigest,
  dockerSecurity,
  
  // Templates
  APP_TEMPLATES,
  TEMPLATE_CATEGORIES,
  DIFFICULTY_LEVELS,
  RECIPE_TEMPLATES,
  RECIPE_CATEGORIES,
  
  // Helpers
  asyncHandler,
  errorResponse,
  ok,
  fetchT,
  httpsAgent,
  log,
  logError,
  safeErrorMessage,
  getServiceById,
  readConfig,
  saveConfig,
  addServiceToConfig,
  validateURL,
  strictLimiter,
  totpConfig,
  saveTotpConfig,
  loadSiteConfig,
  loadNotificationConfig,
  resyncHealthChecker,
  
  // Middleware result
  middlewareResult,
  
  // App
  app,
}) {
  // Create domain-specific contexts
  const docker = createDockerContext(dockerSecurity);
  const caddy = createCaddyContext(CADDYFILE_PATH, CADDY_ADMIN_URL, fetchT, httpsAgent, log, siteConfig, buildDomain);
  const dns = createDnsContext(siteConfig, buildDomain, credentialManager, fetchT, httpsAgent, log, DNS_CREDENTIALS_FILE);
  const session = createSessionContext(middlewareResult);

  // Create notification manager
  const notification = new NotificationManager({
    NOTIFICATIONS_FILE,
    fetchT,
    docker,
    log,
    config: siteConfig
  });

  // Tailscale context (inline for now - could be extracted)
  const tailscale = {
    // These will be populated by server.js for now
    // TODO: Extract tailscale module
  };

  // Assemble flat context (temporary - routes still expect this)
  const ctx = {
    // Namespaced contexts
    docker,
    caddy,
    dns,
    session,
    notification,
    tailscale,
    
    // App and config
    app,
    siteConfig,
    
    // State managers
    servicesStateManager,
    configStateManager,
    
    // Managers
    credentialManager,
    authManager,
    licenseManager,
    healthChecker,
    updateManager,
    backupManager,
    resourceMonitor,
    auditLogger,
    portLockManager,
    selfUpdater,
    dockerMaintenance,
    logDigest,
    
    // Templates
    APP_TEMPLATES,
    TEMPLATE_CATEGORIES,
    DIFFICULTY_LEVELS,
    RECIPE_TEMPLATES,
    RECIPE_CATEGORIES,
    
    // Helpers
    asyncHandler,
    errorResponse,
    ok,
    fetchT,
    log,
    logError,
    safeErrorMessage,
    buildDomain,
    buildServiceUrl,
    getServiceById,
    readConfig,
    saveConfig,
    addServiceToConfig,
    validateURL,
    strictLimiter,
    
    // Config helpers
    totpConfig,
    saveTotpConfig,
    loadSiteConfig,
    loadNotificationConfig,
    resyncHealthChecker,
    
    // Middleware result (exposes renewCSRFToken etc.)
    middlewareResult,
    
    // File paths
    SERVICES_FILE,
    CONFIG_FILE,
    TOTP_CONFIG_FILE,
    TAILSCALE_CONFIG_FILE,
    NOTIFICATIONS_FILE,
    ERROR_LOG_FILE,
  };

  return ctx;
}

module.exports = { assembleContext };
