Skip to content

A self-hosted UPI & COD payment gateway with zero per-transaction platform fees, universal JavaScript checkout integration, dynamic QR payments, WhatsApp OTP authentication, and automated payment verification.

JavaScript
105 KB
Raw Blame
import crypto from 'node:crypto';
import { HttpServer } from '../server.js';
import { PaymentStatus, PaymentMethod, SellingMode, AuditActorType, isValidVpa, toRupeesString, deriveCloudflareRoutes, resolveRuntimeUrls, resolveGmailRedirectUri, createOAuthState, validateOutboundWebhookUrl } from '@youpi/common';
import { PaymentVerificationPipeline } from '@youpi/core';
import { globalTunnelManager } from '../services/tunnel-manager.js';
import { formatQrDataUrl } from '../services/whatsapp-berry.service.js';

function resolveSellingModeAndMethods(body, currentBranding = {}) {
  const inputBranding = body.branding || {};
  let sellingMode = body.sellingMode || inputBranding.sellingMode || currentBranding.sellingMode;
  let rawMethods = body.enabledPaymentMethods || inputBranding.enabledPaymentMethods;

  if (!sellingMode) {
    if (inputBranding.counterInstructions || currentBranding.counterInstructions || body.cashEnabled !== undefined || body.cardEnabled !== undefined) {
      sellingMode = SellingMode.OFFLINE;
    } else if (rawMethods && (rawMethods.includes(PaymentMethod.CASH_COUNTER) || rawMethods.includes(PaymentMethod.CARD_COUNTER))) {
      sellingMode = SellingMode.OFFLINE;
    } else if (currentBranding.enabledPaymentMethods && (currentBranding.enabledPaymentMethods.includes(PaymentMethod.CASH_COUNTER) || currentBranding.enabledPaymentMethods.includes(PaymentMethod.CARD_COUNTER))) {
      sellingMode = SellingMode.OFFLINE;
    } else {
      sellingMode = SellingMode.ONLINE;
    }
  }

  const codOn = body.codEnabled !== undefined 
    ? Boolean(body.codEnabled) 
    : (inputBranding.codEnabled !== undefined 
        ? Boolean(inputBranding.codEnabled) 
        : (currentBranding.codEnabled !== undefined ? Boolean(currentBranding.codEnabled) : (currentBranding.enabledPaymentMethods ? currentBranding.enabledPaymentMethods.includes(PaymentMethod.COD) : true)));

  const cashOn = body.cashEnabled !== undefined 
    ? Boolean(body.cashEnabled) 
    : (inputBranding.cashEnabled !== undefined 
        ? Boolean(inputBranding.cashEnabled) 
        : (currentBranding.cashEnabled !== undefined ? Boolean(currentBranding.cashEnabled) : (currentBranding.enabledPaymentMethods ? currentBranding.enabledPaymentMethods.includes(PaymentMethod.CASH_COUNTER) : true)));

  const cardOn = body.cardEnabled !== undefined 
    ? Boolean(body.cardEnabled) 
    : (inputBranding.cardEnabled !== undefined 
        ? Boolean(inputBranding.cardEnabled) 
        : (currentBranding.cardEnabled !== undefined ? Boolean(currentBranding.cardEnabled) : (currentBranding.enabledPaymentMethods ? currentBranding.enabledPaymentMethods.includes(PaymentMethod.CARD_COUNTER) : true)));

  if (!rawMethods) {
    rawMethods = [PaymentMethod.UPI];
    if (sellingMode === SellingMode.ONLINE) {
      if (codOn) rawMethods.push(PaymentMethod.COD);
    } else {
      if (cashOn) rawMethods.push(PaymentMethod.CASH_COUNTER);
      if (cardOn) rawMethods.push(PaymentMethod.CARD_COUNTER);
    }
  }

  // Strictly filter based on selling mode:
  // UPI is always enabled in both modes! Turning it off isn't allowed!
  const sanitized = [PaymentMethod.UPI];
  if (sellingMode === SellingMode.ONLINE) {
    if (rawMethods.includes(PaymentMethod.COD) && codOn) {
      sanitized.push(PaymentMethod.COD);
    } else if (body.codEnabled === true) {
      sanitized.push(PaymentMethod.COD);
    }
  } else {
    if (rawMethods.includes(PaymentMethod.CASH_COUNTER) && cashOn) {
      sanitized.push(PaymentMethod.CASH_COUNTER);
    } else if (body.cashEnabled === true) {
      sanitized.push(PaymentMethod.CASH_COUNTER);
    }
    if (rawMethods.includes(PaymentMethod.CARD_COUNTER) && cardOn) {
      sanitized.push(PaymentMethod.CARD_COUNTER);
    } else if (body.cardEnabled === true) {
      sanitized.push(PaymentMethod.CARD_COUNTER);
    }
  }

  return {
    sellingMode,
    enabledPaymentMethods: sanitized,
    codEnabled: sanitized.includes(PaymentMethod.COD) || (sellingMode !== SellingMode.ONLINE ? codOn : false),
    cashEnabled: sanitized.includes(PaymentMethod.CASH_COUNTER) || (sellingMode !== SellingMode.OFFLINE ? cashOn : false),
    cardEnabled: sanitized.includes(PaymentMethod.CARD_COUNTER) || (sellingMode !== SellingMode.OFFLINE ? cardOn : false)
  };
}

/**
 * Registers merchant admin dashboard routes.
 * @param {HttpServer} server
 * @param {{
 *   db: import('@youpi/db').Database,
 *   sessionRepo: import('@youpi/db').PaymentSessionRepository,
 *   accountRepo: import('@youpi/db').ReceivingAccountRepository,
 *   merchantRepo: import('@youpi/db').MerchantRepository,
 *   auditRepo: import('@youpi/db').AuditLogRepository,
 *   queueEngine: import('@youpi/core').QueueEngine
 * }} ctx
 */
export function registerAdminRoutes(server, ctx) {
  const resolveMerchant = (req, explicitId = null) => {
    const targetId = explicitId || (req ? (req.query?.merchantId || req.headers?.['x-merchant-id']) : null);
    if (targetId) {
      const found = ctx.merchantRepo.findById(targetId);
      if (found) return found;
    }
    return ctx.merchantRepo.getDefault
      ? ctx.merchantRepo.getDefault()
      : (ctx.merchantRepo.findById('00000000-0000-0000-0000-000000000001') || ctx.merchantRepo.findAll()[0]);
  };

  // GET /api/v1/admin/queue
  server.get('/api/v1/admin/queue', async (req, res) => {
    const merchantId = req.query.merchantId || req.headers?.['x-merchant-id'] || '00000000-0000-0000-0000-000000000001';
    let account = ctx.accountRepo.findActiveForMerchant(merchantId);

    if (!account) {
      const allAccs = ctx.accountRepo.findByMerchantId ? ctx.accountRepo.findByMerchantId(merchantId) : [];
      account = allAccs.find(a => a.isActive) || allAccs[0] || null;
    }

    if (!account) {
      HttpServer.json(res, 200, { active: null, currentSlot: null, queue: [] });
      return;
    }

    let currentActive = ctx.sessionRepo.findCurrentActiveOrProcessing(account.id);
    if (!currentActive && ctx.sessionRepo.findCurrentActiveOrProcessingForMerchant) {
      currentActive = ctx.sessionRepo.findCurrentActiveOrProcessingForMerchant(merchantId);
      if (currentActive && currentActive.receivingAccountId !== account.id) {
        try {
          ctx.db.execute('UPDATE payment_sessions SET receiving_account_id = ? WHERE id = ?', [account.id, currentActive.id]);
          ctx.db.execute('UPDATE payment_receiving_accounts SET is_slot_busy = 1, current_active_session_id = ? WHERE id = ?', [currentActive.id, account.id]);
          currentActive.receivingAccountId = account.id;
        } catch {}
      }
    }

    const queuedSessions = ctx.sessionRepo.findQueuedSessions(account.id);

    HttpServer.json(res, 200, {
      receivingAccount: {
        id: account.id,
        upiId: account.upiId,
        payeeName: account.payeeName,
        isSlotBusy: Boolean(currentActive || account.isSlotBusy)
      },
      currentSlot: currentActive ? {
        id: currentActive.id,
        orderId: currentActive.orderId,
        amountPaise: currentActive.amountPaise.toString(),
        status: currentActive.status,
        expiresAt: currentActive.expiresAt,
        processingExpiresAt: currentActive.processingExpiresAt
      } : null,
      queue: queuedSessions.map(s => ({
        id: s.id,
        orderId: s.orderId,
        amountPaise: s.amountPaise.toString(),
        queuePosition: s.queuePosition,
        createdAt: s.createdAt
      }))
    });
  });

  // GET /api/v1/admin/observers
  server.get('/api/v1/admin/observers', async (req, res) => {
    const devices = ctx.db.query('SELECT * FROM android_devices ORDER BY created_at DESC');
    const now = Date.now();

    const androidHealth = devices.map(d => {
      const lastSeen = d.last_heartbeat_at ? new Date(d.last_heartbeat_at).getTime() : 0;
      const isConnected = (now - lastSeen) < 180000; // Connected if heartbeat within 3 mins
      return {
        id: d.id,
        name: d.device_name,
        model: d.device_model,
        status: isConnected ? 'CONNECTED' : 'DISCONNECTED',
        lastSeenAt: d.last_heartbeat_at,
        lastObservationAt: d.last_observation_at
      };
    });

    // Query live Google Messages browser automator status
    let smsStatus = { status: 'STANDBY', isPaired: false, messagesObserved: 0, isRunning: false };
    try {
      const smsBaseUrl = process.env.SMS_OBSERVER_URL || `http://localhost:${process.env.SMS_OBSERVER_PORT || 6002}`;
      const smsRes = await fetch(new URL('/api/browser/status', smsBaseUrl), {
        headers: { Authorization: `Bearer ${getObserverToken()}` },
        signal: AbortSignal.timeout(1500)
      });
      if (smsRes.ok) {
        smsStatus = await smsRes.json();
      }
    } catch {}

    // Query live Gmail browser automator status
    let gmailStatus = { status: 'STANDBY', isLoggedIn: false, emailsObserved: 0, isRunning: false };
    try {
      const gmailBaseUrl = process.env.GMAIL_OBSERVER_URL || `http://localhost:${process.env.GMAIL_OBSERVER_PORT || 6003}`;
      const gmailRes = await fetch(new URL('/api/browser/status', gmailBaseUrl), {
        headers: { Authorization: `Bearer ${getObserverToken()}` },
        signal: AbortSignal.timeout(1500)
      });
      if (gmailRes.ok) {
        gmailStatus = await gmailRes.json();
      }
    } catch {}

    HttpServer.json(res, 200, {
      gmail: {
        status: gmailStatus.status || (gmailStatus.isLoggedIn ? 'CONNECTED' : 'STANDBY'),
        isLoggedIn: Boolean(gmailStatus.isLoggedIn),
        isRunning: Boolean(gmailStatus.isRunning || gmailStatus.isLoggedIn || gmailStatus.status === 'CONNECTED'),
        emailsObserved: gmailStatus.emailsObserved || 0,
        lastCheckedAt: gmailStatus.lastCheckedAt || null
      },
      android: {
        devices: androidHealth,
        totalDevices: devices.length
      },
      sms: {
        status: smsStatus.status || (smsStatus.isPaired ? 'CONNECTED' : 'STANDBY'),
        isPaired: Boolean(smsStatus.isPaired),
        isRunning: Boolean(smsStatus.isRunning || smsStatus.isPaired || smsStatus.status === 'CONNECTED'),
        messagesObserved: smsStatus.messagesObserved || 0,
        lastSeenAt: smsStatus.lastSeenAt || null
      }
    });
  });

  // GET /api/v1/admin/payments
  server.get('/api/v1/admin/payments', async (req, res) => {
    const merchantId = req.query.merchantId || '00000000-0000-0000-0000-000000000001';
    const limit = parseInt(req.query.limit || '50', 10);
    const payments = ctx.sessionRepo.findByMerchantId(merchantId, limit);

    HttpServer.json(res, 200, {
      payments: payments.map(p => ({
        id: p.id,
        orderId: p.orderId,
        amountPaise: p.amountPaise.toString(),
        currency: p.currency,
        method: p.method,
        status: p.status,
        verificationLevel: p.verificationLevel || 0,
        verificationSources: p.verificationSources || [],
        verificationTag: p.verificationTag || 'UNVERIFIED',
        createdAt: p.createdAt,
        completedAt: p.completedAt
      }))
    });
  });

  // GET /api/v1/admin/tickets
  server.get('/api/v1/admin/tickets', async (req, res) => {
    const merchantId = req.query.merchantId || '00000000-0000-0000-0000-000000000001';
    if (!ctx.ticketRepo) {
      HttpServer.json(res, 200, { tickets: [] });
      return;
    }
    const tickets = ctx.ticketRepo.findTicketsByMerchant(merchantId);
    HttpServer.json(res, 200, { tickets });
  });

  // POST /api/v1/admin/tickets/:id/resolve
  server.post('/api/v1/admin/tickets/:id/resolve', async (req, res, params) => {
    if (!ctx.ticketRepo) {
      HttpServer.json(res, 404, { error: 'Ticket repo not configured' });
      return;
    }
    const ticket = ctx.ticketRepo.updateTicketStatus(params.id, 'RESOLVED');
    HttpServer.json(res, 200, { success: true, ticket });
  });

  // POST /api/v1/admin/payments/:id/confirm (Manual reconciliation for payments pending verification)
  server.post('/api/v1/admin/payments/:id/confirm', async (req, res, params) => {
    const paymentId = params.id;
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const operatorId = body.operatorId || 'admin-operator';
    const notes = body.notes || 'Manually reconciled by admin';

    const session = ctx.sessionRepo.findById(paymentId);
    if (!session) {
      HttpServer.json(res, 404, { error: `Payment session not found: ${paymentId}` });
      return;
    }

    if (session.method === PaymentMethod.MANUAL_UPI) {
      HttpServer.json(res, 409, { error: 'Manual UPI claims must be approved in the merchant order system after checking the receiving bank account.' });
      return;
    }

    if ([PaymentStatus.SUCCESS, PaymentStatus.RECONCILED, PaymentStatus.FAILED, PaymentStatus.EXPIRED, PaymentStatus.CANCELLED].includes(session.status)) {
      HttpServer.json(res, 409, { error: `Cannot confirm a payment in terminal status '${session.status}'.` });
      return;
    }

    const targetStatus = (session.status === PaymentStatus.OBSERVED_SUCCESS || session.status === PaymentStatus.MANUAL_REVIEW)
      ? PaymentStatus.RECONCILED
      : PaymentStatus.SUCCESS;

    const updated = ctx.sessionRepo.transitionToTerminal(paymentId, targetStatus);

    if (ctx.auditRepo) {
      ctx.auditRepo.log({
        merchantId: session.merchantId,
        entityType: 'PAYMENT_SESSION',
        entityId: paymentId,
        action: 'PAYMENT_MANUALLY_RECONCILED',
        actorType: AuditActorType.ADMIN,
        actorId: operatorId,
        details: { operatorId, notes, previousStatus: session.status }
      });
    }

    server.broadcastSse(paymentId, 'status_change', {
      status: targetStatus,
      method: session.method,
      orderId: session.orderId
    });

    if (ctx.eventEmitter) {
      ctx.eventEmitter.emit('PAYMENT.SUCCESS', {
        paymentId: updated.id,
        orderId: session.orderId,
        merchantId: session.merchantId,
        amountPaise: session.amountPaise
      });
    }

    HttpServer.json(res, 200, {
      success: true,
      confirmed: true,
      paymentId,
      status: targetStatus,
      reconciledBy: operatorId
    });
  });

  // POST /api/v1/admin/counter-confirm
  server.post('/api/v1/admin/counter-confirm', async (req, res) => {
    const body = await HttpServer.parseBody(req);
    const { paymentId, operatorId, method } = body;

    if (!paymentId || !operatorId) {
      HttpServer.json(res, 400, { error: 'paymentId and operatorId are required.' });
      return;
    }

    const session = ctx.sessionRepo.findById(paymentId);
    if (!session) {
      HttpServer.json(res, 404, { error: `Payment session not found: ${paymentId}` });
      return;
    }

    // Manual confirmation is only valid for counter payments still awaiting settlement.
    // A UPI session must only reach SUCCESS through the verification engine.
    if (session.method !== PaymentMethod.CASH_COUNTER && session.method !== PaymentMethod.CARD_COUNTER) {
      HttpServer.json(res, 400, { error: `Manual confirmation is only allowed for counter payments (session method is '${session.method}').` });
      return;
    }
    if ([PaymentStatus.SUCCESS, PaymentStatus.FAILED, PaymentStatus.EXPIRED, PaymentStatus.CANCELLED].includes(session.status)) {
      HttpServer.json(res, 409, { error: `Cannot confirm a payment in terminal status '${session.status}'.` });
      return;
    }

    const updated = ctx.sessionRepo.transitionToTerminal(paymentId, PaymentStatus.SUCCESS);

    ctx.auditRepo.log({
      merchantId: session.merchantId,
      entityType: 'PAYMENT_SESSION',
      entityId: paymentId,
      action: 'COUNTER_MANUAL_PAYMENT_CONFIRMED',
      actorType: AuditActorType.ADMIN,
      actorId: operatorId,
      details: { method: method || PaymentMethod.CASH_COUNTER, operatorId }
    });

    server.broadcastSse(paymentId, 'status_change', {
      status: PaymentStatus.SUCCESS,
      method: method || PaymentMethod.CASH_COUNTER,
      orderId: session.orderId
    });
    server.broadcastSse(paymentId, 'state_change', {
      status: PaymentStatus.SUCCESS,
      method: method || PaymentMethod.CASH_COUNTER,
      orderId: session.orderId
    });

    if (ctx.eventEmitter) {
      ctx.eventEmitter.emit('PAYMENT.SUCCESS', {
        paymentId: updated.id,
        orderId: session.orderId,
        merchantId: session.merchantId,
        amountPaise: session.amountPaise
      });
    }

    HttpServer.json(res, 200, {
      confirmed: true,
      paymentId: updated.id,
      status: PaymentStatus.SUCCESS,
      orderId: session.orderId
    });
  });

  // POST /api/v1/admin/counter-decline (Declines/Rejects manual cash or card counter payment)
  server.post('/api/v1/admin/counter-decline', async (req, res) => {
    const body = await HttpServer.parseBody(req);
    const { paymentId, operatorId, reason } = body;

    if (!paymentId || !operatorId) {
      HttpServer.json(res, 400, { error: 'paymentId and operatorId are required.' });
      return;
    }

    const session = ctx.sessionRepo.findById(paymentId);
    if (!session) {
      HttpServer.json(res, 404, { error: `Payment session not found: ${paymentId}` });
      return;
    }

    const updated = ctx.sessionRepo.transitionToTerminal(paymentId, PaymentStatus.FAILED);

    ctx.auditRepo.log({
      merchantId: session.merchantId,
      entityType: 'PAYMENT_SESSION',
      entityId: paymentId,
      action: 'COUNTER_MANUAL_PAYMENT_DECLINED',
      actorType: AuditActorType.ADMIN,
      actorId: operatorId,
      details: {
        method: session.method,
        operatorId,
        reason: reason || 'Declined by merchant counter operator'
      }
    });

    server.broadcastSse(paymentId, 'status_change', {
      status: PaymentStatus.FAILED,
      method: session.method,
      reason: reason || 'Declined by merchant counter operator',
      orderId: session.orderId
    });
    server.broadcastSse(paymentId, 'state_change', {
      status: PaymentStatus.FAILED,
      method: session.method,
      reason: reason || 'Declined by merchant counter operator',
      orderId: session.orderId
    });

    if (ctx.eventEmitter) {
      ctx.eventEmitter.emit('PAYMENT.FAILED', {
        paymentId: updated.id,
        orderId: session.orderId,
        merchantId: session.merchantId,
        amountPaise: session.amountPaise,
        reason: reason || 'Counter payment declined'
      });
    }

    HttpServer.json(res, 200, {
      declined: true,
      paymentId: updated.id,
      status: PaymentStatus.FAILED,
      reason: reason || 'Counter payment declined'
    });
  });

  // GET /api/v1/admin/upi-settings
  server.get('/api/v1/admin/upi-settings', async (req, res) => {
    let merchant = resolveMerchant(req);

    const account = merchant ? ctx.accountRepo.findActiveForMerchant(merchant.id) : null;
    const accounts = merchant ? ctx.accountRepo.findByMerchantId(merchant.id) : [];

    if (!account) {
      HttpServer.json(res, 200, {
        id: '',
        upiId: '',
        payeeName: '',
        isMerchantEnabled: true,
        isActive: false,
        accounts
      });
      return;
    }
    HttpServer.json(res, 200, {
      id: account.id,
      upiId: account.upiId,
      payeeName: account.payeeName,
      isMerchantEnabled: account.isMerchantEnabled,
      isActive: account.isActive,
      accounts
    });
  });

  // GET /api/v1/admin/upi-accounts (List all merchant VPAs)
  server.get('/api/v1/admin/upi-accounts', async (req, res) => {
    let merchant = resolveMerchant(req);

    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const accounts = ctx.accountRepo.findByMerchantId(merchant.id);
    HttpServer.json(res, 200, {
      accounts: accounts.map(a => ({
        id: a.id,
        merchantId: a.merchantId,
        upiId: a.upiId,
        payeeName: a.payeeName,
        isMerchantEnabled: a.isMerchantEnabled,
        isActive: a.isActive,
        isSlotBusy: a.isSlotBusy,
        createdAt: a.createdAt,
        updatedAt: a.updatedAt
      }))
    });
  });

  // POST /api/v1/admin/upi-accounts (Add a new VPA)
  server.post('/api/v1/admin/upi-accounts', async (req, res) => {
    const body = await HttpServer.parseBody(req);
    const { upiId, payeeName, isActive } = body;
    let merchant = resolveMerchant(req, body.merchantId);

    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    if (!upiId || !upiId.trim()) {
      HttpServer.json(res, 400, { error: 'UPI ID (VPA) is required.' });
      return;
    }

    if (!isValidVpa(upiId.trim())) {
      HttpServer.json(res, 400, { error: `Invalid UPI VPA format: "${upiId}". Expected: username@bank` });
      return;
    }

    const existingAccounts = ctx.accountRepo.findByMerchantId(merchant.id);
    const shouldActivate = isActive !== undefined ? Boolean(isActive) : (existingAccounts.length === 0);

    const account = ctx.accountRepo.create({
      merchantId: merchant.id,
      upiId: upiId.trim(),
      payeeName: payeeName?.trim() || merchant.businessName,
      isMerchantEnabled: true,
      isActive: shouldActivate
    });

    ctx.auditRepo.log({
      merchantId: merchant.id,
      entityType: 'PAYMENT_RECEIVING_ACCOUNT',
      entityId: account.id,
      action: 'RECEIVING_UPI_ADDED',
      actorType: AuditActorType.ADMIN,
      details: {
        upiId: account.upiId,
        payeeName: account.payeeName,
        isActive: account.isActive
      }
    });

    HttpServer.json(res, 201, {
      success: true,
      account
    });
  });

  // POST /api/v1/admin/upi-accounts/:id/activate (Atomically activate VPA, deactivating all others)
  server.post('/api/v1/admin/upi-accounts/:id/activate', async (req, res, params) => {
    const account = ctx.accountRepo.findById(params.id);
    if (!account) {
      HttpServer.json(res, 404, { error: `UPI Account not found: ${params.id}` });
      return;
    }

    const updated = ctx.accountRepo.activateAccount(account.merchantId, account.id);

    ctx.auditRepo.log({
      merchantId: account.merchantId,
      entityType: 'PAYMENT_RECEIVING_ACCOUNT',
      entityId: updated.id,
      action: 'RECEIVING_UPI_ACTIVATED',
      actorType: AuditActorType.ADMIN,
      details: {
        upiId: updated.upiId,
        payeeName: updated.payeeName,
        isMerchantEnabled: updated.isMerchantEnabled
      }
    });

    HttpServer.json(res, 200, {
      success: true,
      activeAccount: updated
    });
  });

  // POST /api/v1/admin/upi-accounts/:id/toggle (Toggle VPA active state)
  server.post('/api/v1/admin/upi-accounts/:id/toggle', async (req, res, params) => {
    const account = ctx.accountRepo.findById(params.id);
    if (!account) {
      HttpServer.json(res, 404, { error: `UPI Account not found: ${params.id}` });
      return;
    }

    let updated;
    if (account.isActive) {
      updated = ctx.accountRepo.deactivateAccount(account.merchantId, account.id);
    } else {
      updated = ctx.accountRepo.activateAccount(account.merchantId, account.id);
    }

    ctx.auditRepo.log({
      merchantId: account.merchantId,
      entityType: 'PAYMENT_RECEIVING_ACCOUNT',
      entityId: updated.id,
      action: updated.isActive ? 'RECEIVING_UPI_ACTIVATED' : 'RECEIVING_UPI_DEACTIVATED',
      actorType: AuditActorType.ADMIN,
      details: { upiId: updated.upiId, isActive: updated.isActive }
    });

    HttpServer.json(res, 200, {
      success: true,
      account: updated
    });
  });

  // DELETE /api/v1/admin/upi-accounts/:id (Delete VPA)
  server.delete('/api/v1/admin/upi-accounts/:id', async (req, res, params) => {
    const account = ctx.accountRepo.findById(params.id);
    if (!account) {
      HttpServer.json(res, 404, { error: `UPI Account not found: ${params.id}` });
      return;
    }

    try {
      ctx.accountRepo.deleteAccount(account.merchantId, account.id);
      ctx.auditRepo.log({
        merchantId: account.merchantId,
        entityType: 'PAYMENT_RECEIVING_ACCOUNT',
        entityId: account.id,
        action: 'RECEIVING_UPI_DELETED',
        actorType: AuditActorType.ADMIN,
        details: { upiId: account.upiId }
      });
      HttpServer.json(res, 200, { success: true, deletedId: account.id });
    } catch (err) {
      HttpServer.json(res, 400, { error: err.message });
    }
  });

  // POST /api/v1/admin/upi-settings (Custom UPI ID addition & persistence)
  server.post('/api/v1/admin/upi-settings', async (req, res) => {
    const body = await HttpServer.parseBody(req);
    const { upiId, payeeName, isMerchantEnabled } = body;
    let merchant = resolveMerchant(req, body.merchantId);

    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    if (!upiId || !upiId.trim()) {
      HttpServer.json(res, 400, { error: 'UPI ID (VPA) is required.' });
      return;
    }

    if (!isValidVpa(upiId.trim())) {
      HttpServer.json(res, 400, { error: `Invalid UPI VPA format: "${upiId}". Expected format: username@bank` });
      return;
    }

    const isEnabled = isMerchantEnabled !== undefined ? Boolean(isMerchantEnabled) : true;
    const updatedAccount = ctx.accountRepo.upsertActiveForMerchant(
      merchant.id,
      upiId.trim(),
      payeeName?.trim() || merchant.businessName,
      isEnabled
    );

    ctx.auditRepo.log({
      merchantId: merchant.id,
      entityType: 'PAYMENT_RECEIVING_ACCOUNT',
      entityId: updatedAccount.id,
      action: 'RECEIVING_UPI_UPDATED',
      actorType: AuditActorType.ADMIN,
      details: {
        upiId: updatedAccount.upiId,
        payeeName: updatedAccount.payeeName,
        isMerchantEnabled: updatedAccount.isMerchantEnabled
      }
    });

    HttpServer.json(res, 200, {
      success: true,
      receivingAccount: {
        id: updatedAccount.id,
        upiId: updatedAccount.upiId,
        payeeName: updatedAccount.payeeName,
        isMerchantEnabled: updatedAccount.isMerchantEnabled,
        isActive: updatedAccount.isActive
      }
    });
  });

  // GET /api/v1/admin/all-settings (Unified Settings Read)
  server.get('/api/v1/admin/all-settings', async (req, res) => {
    let merchant = resolveMerchant(req);

    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const account = ctx.accountRepo.findActiveForMerchant(merchant.id);
    const branding = merchant.branding || {};
    const modeResult = resolveSellingModeAndMethods({}, branding);
    const sellingMode = modeResult.sellingMode;
    const enabledPaymentMethods = modeResult.enabledPaymentMethods;
    const supportEmails = branding.supportEmails || (merchant.contactEmail ? [merchant.contactEmail] : []);
    const supportMobiles = branding.supportMobiles || (merchant.contactPhone ? [merchant.contactPhone] : []);
    const privacyPolicyUrl = branding.privacyPolicyUrl || '';

    HttpServer.json(res, 200, {
      merchantId: merchant.id,
      publishableKey: merchant.id,
      merchantKey: merchant.merchantKey || branding.merchantApiKey || '',
      businessName: merchant.businessName,
      upiId: account ? account.upiId : '',
      payeeName: account ? account.payeeName : merchant.businessName,
      webhookUrl: merchant.webhookUrl || '',
      webhookSecret: merchant.webhookSecret || '',
      supportEmails,
      supportMobiles,
      privacyPolicyUrl,
      sellingMode,
      branding: {
        appName: branding.appName || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG',
        primaryColor: branding.primaryColor || (process.env.DEFAULT_PRIMARY_COLOR || '').trim() || '#2563eb',
        headerTheme: branding.headerTheme || 'gradient',
        logoUrl: branding.logoUrl !== undefined ? branding.logoUrl : ((process.env.DEFAULT_LOGO_URL || '').trim() || null),
        sidebarNotice: branding.sidebarNotice || 'Fast & Secure 256-Bit Encrypted Payments',
        subbannerText: branding.subbannerText || null,
        trustBadgeText: branding.trustBadgeText || (process.env.DEFAULT_TRUST_BADGE_TEXT || '').trim() || null,
        hideTrustBadge: Boolean(branding.hideTrustBadge),
        hideSubbanner: Boolean(branding.hideSubbanner),
        hidePoweredBy: branding.hidePoweredBy !== undefined ? Boolean(branding.hidePoweredBy) : (process.env.DEFAULT_HIDE_POWERED_BY === 'true'),
        hideTimeSavingBanner: Boolean(branding.hideTimeSavingBanner),
        footerGuarantee: branding.footerGuarantee || (process.env.DEFAULT_FOOTER_GUARANTEE || '').trim() || `Secured by ${branding.appName || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG'} 256-Bit Encryption`,
        counterInstructions: branding.counterInstructions || 'Please visit the store billing counter and present this screen to the cashier.',
        cardCounterInstructions: branding.cardCounterInstructions || 'Please swipe, insert, or tap your credit or debit card on the store POS terminal at the billing desk.',
        termsUrl: branding.termsUrl || null,
        sellingMode,
        enabledPaymentMethods,
        supportEmails,
        supportMobiles,
        privacyPolicyUrl
      }
    });
  });

  // POST /api/v1/admin/all-settings (Atomic Unified Settings Write)
  server.post('/api/v1/admin/all-settings', async (req, res) => {
    const body = await HttpServer.parseBody(req);
    let merchant = resolveMerchant(req, body.merchantId);

    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    // 1. Update UPI VPA if provided
    let account = ctx.accountRepo.findActiveForMerchant(merchant.id);
    if (body.upiId && body.upiId.trim()) {
      if (!isValidVpa(body.upiId.trim())) {
        HttpServer.json(res, 400, { error: `Invalid UPI VPA format: "${body.upiId}". Expected: username@bank` });
        return;
      }
      account = ctx.accountRepo.upsertActiveForMerchant(merchant.id, body.upiId.trim(), body.payeeName?.trim() || body.businessName?.trim() || merchant.businessName);
    }

    // 2. Non-Destructive Deep Merge for Branding & Merchant Properties
    const updates = {};
    if (body.businessName && body.businessName.trim()) {
      updates.businessName = body.businessName.trim();
    }
    if (body.webhookUrl !== undefined) {
      const check = validateOutboundWebhookUrl(body.webhookUrl);
      if (!check.valid && String(body.webhookUrl || '').trim()) {
        HttpServer.json(res, 400, { error: check.error });
        return;
      }
      updates.webhookUrl = body.webhookUrl?.trim() || null;
    }
    if (body.webhookSecret !== undefined && body.webhookSecret.trim()) {
      updates.webhookSecret = body.webhookSecret.trim();
    }

    const currentBranding = merchant.branding || {};
    const inputBranding = body.branding || {};
    const mergedBranding = { ...currentBranding };

    if (inputBranding.appName !== undefined) mergedBranding.appName = inputBranding.appName.trim() || currentBranding.appName || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG';
    if (inputBranding.logoUrl !== undefined) mergedBranding.logoUrl = inputBranding.logoUrl ? inputBranding.logoUrl.trim() : null;
    if (inputBranding.primaryColor !== undefined) mergedBranding.primaryColor = inputBranding.primaryColor.trim() || currentBranding.primaryColor || (process.env.DEFAULT_PRIMARY_COLOR || '').trim() || '#2563eb';
    if (inputBranding.sidebarNotice !== undefined) mergedBranding.sidebarNotice = inputBranding.sidebarNotice ? inputBranding.sidebarNotice.trim() : null;
    if (inputBranding.headerTheme !== undefined) mergedBranding.headerTheme = inputBranding.headerTheme || 'gradient';
    if (inputBranding.subbannerText !== undefined) mergedBranding.subbannerText = inputBranding.subbannerText ? inputBranding.subbannerText.trim() : null;
    if (inputBranding.trustBadgeText !== undefined) mergedBranding.trustBadgeText = inputBranding.trustBadgeText ? inputBranding.trustBadgeText.trim() : null;
    if (inputBranding.hideTrustBadge !== undefined) mergedBranding.hideTrustBadge = Boolean(inputBranding.hideTrustBadge);
    if (inputBranding.hideSubbanner !== undefined) mergedBranding.hideSubbanner = Boolean(inputBranding.hideSubbanner);
    if (inputBranding.hidePoweredBy !== undefined) mergedBranding.hidePoweredBy = Boolean(inputBranding.hidePoweredBy);
    if (inputBranding.hideTimeSavingBanner !== undefined) mergedBranding.hideTimeSavingBanner = Boolean(inputBranding.hideTimeSavingBanner);
    if (inputBranding.checkoutNotice !== undefined) mergedBranding.checkoutNotice = inputBranding.checkoutNotice ? inputBranding.checkoutNotice.trim() : null;
    if (inputBranding.footerGuarantee !== undefined) mergedBranding.footerGuarantee = inputBranding.footerGuarantee ? inputBranding.footerGuarantee.trim() : null;
    if (inputBranding.counterInstructions !== undefined) mergedBranding.counterInstructions = inputBranding.counterInstructions ? inputBranding.counterInstructions.trim() : null;
    if (inputBranding.cardCounterInstructions !== undefined) mergedBranding.cardCounterInstructions = inputBranding.cardCounterInstructions ? inputBranding.cardCounterInstructions.trim() : null;
    if (inputBranding.termsUrl !== undefined) mergedBranding.termsUrl = inputBranding.termsUrl ? inputBranding.termsUrl.trim() : null;

    if (body.enabledPaymentMethods || inputBranding.enabledPaymentMethods || body.sellingMode || inputBranding.sellingMode || body.codEnabled !== undefined || body.cashEnabled !== undefined || body.cardEnabled !== undefined) {
      const modeResult = resolveSellingModeAndMethods(body, currentBranding);
      mergedBranding.sellingMode = modeResult.sellingMode;
      mergedBranding.enabledPaymentMethods = modeResult.enabledPaymentMethods;
    }

    if (body.supportEmails !== undefined || inputBranding.supportEmails !== undefined) {
      const raw = body.supportEmails !== undefined ? body.supportEmails : inputBranding.supportEmails;
      mergedBranding.supportEmails = Array.isArray(raw)
        ? raw.map(s => String(s).trim()).filter(Boolean)
        : (typeof raw === 'string' ? raw.split(/[,\n]+/).map(s => s.trim()).filter(Boolean) : []);
    }
    if (body.supportMobiles !== undefined || inputBranding.supportMobiles !== undefined) {
      const raw = body.supportMobiles !== undefined ? body.supportMobiles : inputBranding.supportMobiles;
      mergedBranding.supportMobiles = Array.isArray(raw)
        ? raw.map(s => String(s).trim()).filter(Boolean)
        : (typeof raw === 'string' ? raw.split(/[,\n]+/).map(s => s.trim()).filter(Boolean) : []);
    }
    if (body.privacyPolicyUrl !== undefined || inputBranding.privacyPolicyUrl !== undefined) {
      const raw = body.privacyPolicyUrl !== undefined ? body.privacyPolicyUrl : inputBranding.privacyPolicyUrl;
      mergedBranding.privacyPolicyUrl = raw ? String(raw).trim() : null;
    }

    if (body.envMode !== undefined || inputBranding.envMode !== undefined) {
      mergedBranding.envMode = (body.envMode || inputBranding.envMode || 'localhost').toLowerCase();
    }
    if (body.domainPrefix !== undefined || inputBranding.domainPrefix !== undefined) {
      mergedBranding.domainPrefix = (body.domainPrefix || inputBranding.domainPrefix || 'pay').trim().toLowerCase();
    }
    if (body.domainTld !== undefined || inputBranding.domainTld !== undefined) {
      mergedBranding.domainTld = (body.domainTld || inputBranding.domainTld || '').trim().toLowerCase();
    }
    if (body.customDomain !== undefined || inputBranding.customDomain !== undefined) {
      mergedBranding.customDomain = (body.customDomain || inputBranding.customDomain || '').trim();
    }

    updates.branding = mergedBranding;

    const updated = ctx.merchantRepo.update(merchant.id, updates);

    ctx.auditRepo.log({
      merchantId: merchant.id,
      entityType: 'MERCHANT',
      entityId: merchant.id,
      action: 'ALL_SETTINGS_UPDATED_ATOMIC',
      actorType: AuditActorType.ADMIN,
      details: { businessName: updated.businessName, upiId: account?.upiId, branding: updated.branding }
    });

    HttpServer.json(res, 200, {
      success: true,
      merchantId: updated.id,
      businessName: updated.businessName,
      upiId: account ? account.upiId : '',
      payeeName: account ? account.payeeName : updated.businessName,
      webhookUrl: updated.webhookUrl || '',
      webhookSecret: updated.webhookSecret || '',
      supportEmails: updated.branding?.supportEmails || [],
      supportMobiles: updated.branding?.supportMobiles || [],
      privacyPolicyUrl: updated.branding?.privacyPolicyUrl || '',
      sellingMode: updated.branding?.sellingMode || 'ONLINE',
      enabledPaymentMethods: updated.branding?.enabledPaymentMethods || ['UPI'],
      codEnabled: (updated.branding?.enabledPaymentMethods || []).includes('COD'),
      cashEnabled: (updated.branding?.enabledPaymentMethods || []).includes('CASH_COUNTER'),
      cardEnabled: (updated.branding?.enabledPaymentMethods || []).includes('CARD_COUNTER'),
      branding: updated.branding
    });
  });

  // GET /api/v1/admin/merchants (List all registered merchants)
  server.get('/api/v1/admin/merchants', async (req, res) => {
    const merchants = ctx.merchantRepo.findAll();
    const list = merchants.map(m => ({
      id: m.id,
      businessName: m.businessName,
      slug: m.slug,
      contactEmail: m.contactEmail,
      contactPhone: m.contactPhone,
      webhookUrl: m.webhookUrl,
      apiKeyConfigured: Boolean(m.apiKeyHash),
      createdAt: m.createdAt,
      updatedAt: m.updatedAt
    }));
    HttpServer.json(res, 200, { merchants: list });
  });

  // POST /api/v1/admin/merchants (Create/Onboard a new merchant)
  server.post('/api/v1/admin/merchants', async (req, res) => {
    const body = await HttpServer.parseBody(req);
    if (!body.businessName || !body.businessName.trim()) {
      HttpServer.json(res, 400, { error: 'businessName is required' });
      return;
    }
    const businessName = body.businessName.trim();
    const slugCandidate = body.slug?.trim() || businessName.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
    const slug = slugCandidate || `merchant-${crypto.randomBytes(4).toString('hex')}`;

    const existing = ctx.merchantRepo.findBySlug(slug);
    if (existing) {
      HttpServer.json(res, 409, { error: `Merchant with slug '${slug}' already exists` });
      return;
    }

    if (body.webhookUrl) {
      const check = validateOutboundWebhookUrl(body.webhookUrl);
      if (!check.valid) {
        HttpServer.json(res, 400, { error: check.error });
        return;
      }
    }

    const rawApiKey = body.apiKey?.trim() || `yp_live_${crypto.randomBytes(16).toString('hex')}`;
    const rawWebhookSecret = body.webhookSecret?.trim() || crypto.randomBytes(32).toString('hex');

    const merchant = ctx.merchantRepo.create({
      businessName,
      slug,
      contactEmail: body.contactEmail?.trim() || '',
      contactPhone: body.contactPhone?.trim() || '',
      webhookUrl: body.webhookUrl?.trim() || null,
      webhookSecret: rawWebhookSecret,
      apiKey: rawApiKey
    });

    if (body.upiId || body.vpa) {
      const upiCandidate = String(body.upiId || body.vpa).trim();
      if (isValidVpa(upiCandidate)) {
        ctx.accountRepo.create({
          merchantId: merchant.id,
          upiId: upiCandidate,
          payeeName: businessName,
          isActive: true
        });
      }
    }

    HttpServer.json(res, 201, {
      merchant: {
        id: merchant.id,
        businessName: merchant.businessName,
        slug: merchant.slug,
        contactEmail: merchant.contactEmail,
        contactPhone: merchant.contactPhone,
        webhookUrl: merchant.webhookUrl,
        apiKey: rawApiKey,
        webhookSecret: rawWebhookSecret,
        createdAt: merchant.createdAt
      }
    });
  });

  // POST /api/v1/admin/merchants/:id/rotate-key (Rotate API key for a merchant)
  server.post('/api/v1/admin/merchants/:id/rotate-key', async (req, res, params) => {
    const merchant = ctx.merchantRepo.findById(params.id);
    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }
    const newApiKey = `yp_live_${crypto.randomBytes(16).toString('hex')}`;
    ctx.merchantRepo.update(merchant.id, { apiKey: newApiKey });
    HttpServer.json(res, 200, {
      merchantId: merchant.id,
      apiKey: newApiKey,
      message: 'Merchant API key successfully rotated'
    });
  });

  // GET /api/v1/admin/merchant-settings
  server.get('/api/v1/admin/merchant-settings', async (req, res) => {
    let merchant = req.query.merchantId ? ctx.merchantRepo.findById(req.query.merchantId) : (req.headers['x-merchant-id'] ? ctx.merchantRepo.findById(req.headers['x-merchant-id']) : null);
    if (!merchant) merchant = ctx.merchantRepo.getDefault ? ctx.merchantRepo.getDefault() : (ctx.merchantRepo.findById('00000000-0000-0000-0000-000000000001') || ctx.merchantRepo.findAll()[0]);

    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const branding = merchant.branding || {};
    const modeResult = resolveSellingModeAndMethods({}, branding);
    const sellingMode = modeResult.sellingMode;
    const enabledPaymentMethods = modeResult.enabledPaymentMethods;
    const supportEmails = branding.supportEmails || (merchant.contactEmail ? [merchant.contactEmail] : []);
    const supportMobiles = branding.supportMobiles || (merchant.contactPhone ? [merchant.contactPhone] : []);
    const privacyPolicyUrl = branding.privacyPolicyUrl || '';

    const envMode = branding.envMode || 'localhost';
    const domainPrefix = (branding.deploymentPrefix || branding.domainPrefix || 'pay').trim().toLowerCase();
    const domainTld = (branding.cloudflareDomain || branding.domainTld || '').trim().toLowerCase();
    const cloudflareDomain = domainTld;
    const deploymentPrefix = domainPrefix;
    const customDomain = branding.customDomain || (cloudflareDomain ? `https://${deploymentPrefix}.${cloudflareDomain}` : '');
    const cloudflareTunnelCommand = branding.cloudflareTunnelCommand || '';
    const tunnelStatus = globalTunnelManager.getStatus();

    const routes = deriveCloudflareRoutes({ domain: cloudflareDomain, prefix: deploymentPrefix });
    let runtimeUrls = {};
    try {
      runtimeUrls = resolveRuntimeUrls({ envMode, domain: cloudflareDomain, prefix: deploymentPrefix });
    } catch {
      runtimeUrls = resolveRuntimeUrls({ envMode: 'localhost', domain: cloudflareDomain, prefix: deploymentPrefix });
    }

    const codEnabled = branding.codEnabled !== undefined ? Boolean(branding.codEnabled) : enabledPaymentMethods.includes('COD');
    const cashEnabled = branding.cashEnabled !== undefined ? Boolean(branding.cashEnabled) : enabledPaymentMethods.includes('CASH_COUNTER');
    const cardEnabled = branding.cardEnabled !== undefined ? Boolean(branding.cardEnabled) : enabledPaymentMethods.includes('CARD_COUNTER');

    HttpServer.json(res, 200, {
      merchantId: merchant.id,
      publishableKey: merchant.id,
      merchantKey: merchant.merchantKey || branding.merchantApiKey || '',
      businessName: merchant.businessName,
      contactEmail: merchant.contactEmail,
      contactPhone: merchant.contactPhone,
      webhookUrl: merchant.webhookUrl,
      webhookSecret: merchant.webhookSecret,
      slug: merchant.slug,
      apiKeyConfigured: Boolean(merchant.apiKeyHash),
      supportEmails,
      supportMobiles,
      privacyPolicyUrl,
      sellingMode,
      enabledPaymentMethods,
      codEnabled,
      cashEnabled,
      cardEnabled,
      envMode,
      domainPrefix,
      domainTld,
      cloudflareDomain,
      deploymentPrefix,
      customDomain,
      routes,
      runtimeUrls,
      cloudflareTunnelCommand,
      tunnelStatus,
      branding: {
        ...branding,
        sellingMode,
        enabledPaymentMethods,
        codEnabled,
        cashEnabled,
        cardEnabled,
        supportEmails,
        supportMobiles,
        privacyPolicyUrl,
        envMode,
        domainPrefix,
        domainTld,
        cloudflareDomain,
        deploymentPrefix,
        customDomain,
        routes,
        runtimeUrls,
        cloudflareTunnelCommand
      }
    });
  });

  // POST /api/v1/admin/merchant-settings
  server.post('/api/v1/admin/merchant-settings', async (req, res) => {
    const body = await HttpServer.parseBody(req);
    let merchant = body.merchantId ? ctx.merchantRepo.findById(body.merchantId) : (req.headers['x-merchant-id'] ? ctx.merchantRepo.findById(req.headers['x-merchant-id']) : null);
    if (!merchant) merchant = ctx.merchantRepo.getDefault ? ctx.merchantRepo.getDefault() : (ctx.merchantRepo.findById('00000000-0000-0000-0000-000000000001') || ctx.merchantRepo.findAll()[0]);

    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const inputBranding = body.branding || {};
    const updates = {};
    if (body.businessName && body.businessName.trim()) updates.businessName = body.businessName.trim();
    else if (inputBranding.businessName && inputBranding.businessName.trim()) updates.businessName = inputBranding.businessName.trim();
    if (body.webhookUrl !== undefined) {
      const check = validateOutboundWebhookUrl(body.webhookUrl);
      if (!check.valid && String(body.webhookUrl || '').trim()) {
        HttpServer.json(res, 400, { error: check.error });
        return;
      }
      updates.webhookUrl = body.webhookUrl?.trim() || null;
    } else if (inputBranding.webhookUrl !== undefined) updates.webhookUrl = inputBranding.webhookUrl?.trim() || null;
    if (body.webhookSecret !== undefined && body.webhookSecret.trim()) updates.webhookSecret = body.webhookSecret.trim();

    if (
      body.branding ||
      body.enabledPaymentMethods ||
      body.sellingMode ||
      body.codEnabled !== undefined ||
      body.cashEnabled !== undefined ||
      body.cardEnabled !== undefined ||
      body.supportEmails !== undefined ||
      body.supportMobiles !== undefined ||
      body.privacyPolicyUrl !== undefined ||
      body.envMode !== undefined ||
      body.domainPrefix !== undefined ||
      body.domainTld !== undefined ||
      body.customDomain !== undefined ||
      body.cloudflareTunnelCommand !== undefined
    ) {
      const currentBranding = merchant.branding || {};
      const mergedBranding = { ...currentBranding };

      if (inputBranding.appName !== undefined) mergedBranding.appName = inputBranding.appName.trim() || currentBranding.appName || (process.env.DEFAULT_APP_NAME || '').trim() || 'YOUPI PG';
      if (inputBranding.logoUrl !== undefined) mergedBranding.logoUrl = inputBranding.logoUrl ? inputBranding.logoUrl.trim() : null;
      if (inputBranding.primaryColor !== undefined) mergedBranding.primaryColor = inputBranding.primaryColor.trim() || currentBranding.primaryColor || (process.env.DEFAULT_PRIMARY_COLOR || '').trim() || '#2563eb';
      if (inputBranding.headerTheme !== undefined) mergedBranding.headerTheme = inputBranding.headerTheme || 'gradient';
      if (inputBranding.sidebarNotice !== undefined) mergedBranding.sidebarNotice = inputBranding.sidebarNotice ? inputBranding.sidebarNotice.trim() : null;
      if (inputBranding.subbannerText !== undefined) mergedBranding.subbannerText = inputBranding.subbannerText ? inputBranding.subbannerText.trim() : null;
      if (inputBranding.trustBadgeText !== undefined) mergedBranding.trustBadgeText = inputBranding.trustBadgeText ? inputBranding.trustBadgeText.trim() : null;
      if (inputBranding.hideTrustBadge !== undefined) mergedBranding.hideTrustBadge = Boolean(inputBranding.hideTrustBadge);
      if (inputBranding.hideSubbanner !== undefined) mergedBranding.hideSubbanner = Boolean(inputBranding.hideSubbanner);
      if (inputBranding.hidePoweredBy !== undefined) mergedBranding.hidePoweredBy = Boolean(inputBranding.hidePoweredBy);
      if (inputBranding.hideTimeSavingBanner !== undefined) mergedBranding.hideTimeSavingBanner = Boolean(inputBranding.hideTimeSavingBanner);
      if (inputBranding.checkoutNotice !== undefined) mergedBranding.checkoutNotice = inputBranding.checkoutNotice ? inputBranding.checkoutNotice.trim() : null;
      if (inputBranding.footerGuarantee !== undefined) mergedBranding.footerGuarantee = inputBranding.footerGuarantee ? inputBranding.footerGuarantee.trim() : null;
      if (inputBranding.counterInstructions !== undefined) mergedBranding.counterInstructions = inputBranding.counterInstructions ? inputBranding.counterInstructions.trim() : null;
      if (inputBranding.cardCounterInstructions !== undefined) mergedBranding.cardCounterInstructions = inputBranding.cardCounterInstructions ? inputBranding.cardCounterInstructions.trim() : null;
      if (inputBranding.termsUrl !== undefined) mergedBranding.termsUrl = inputBranding.termsUrl ? inputBranding.termsUrl.trim() : null;

      if (
        body.sellingMode !== undefined ||
        body.enabledPaymentMethods !== undefined ||
        body.codEnabled !== undefined ||
        body.cashEnabled !== undefined ||
        body.cardEnabled !== undefined ||
        inputBranding.sellingMode !== undefined ||
        inputBranding.enabledPaymentMethods !== undefined ||
        inputBranding.codEnabled !== undefined ||
        inputBranding.cashEnabled !== undefined ||
        inputBranding.cardEnabled !== undefined
      ) {
        const modeResult = resolveSellingModeAndMethods(body, currentBranding);
        mergedBranding.sellingMode = modeResult.sellingMode;
        mergedBranding.enabledPaymentMethods = modeResult.enabledPaymentMethods;
        mergedBranding.codEnabled = modeResult.codEnabled;
        mergedBranding.cashEnabled = modeResult.cashEnabled;
        mergedBranding.cardEnabled = modeResult.cardEnabled;
      }

      if (body.supportEmails !== undefined || inputBranding.supportEmails !== undefined) {
        const raw = body.supportEmails !== undefined ? body.supportEmails : inputBranding.supportEmails;
        mergedBranding.supportEmails = Array.isArray(raw)
          ? raw.map(s => String(s).trim()).filter(Boolean)
          : (typeof raw === 'string' ? raw.split(/[,\n]+/).map(s => s.trim()).filter(Boolean) : []);
      }
      if (body.supportMobiles !== undefined || inputBranding.supportMobiles !== undefined) {
        const raw = body.supportMobiles !== undefined ? body.supportMobiles : inputBranding.supportMobiles;
        mergedBranding.supportMobiles = Array.isArray(raw)
          ? raw.map(s => String(s).trim()).filter(Boolean)
          : (typeof raw === 'string' ? raw.split(/[,\n]+/).map(s => s.trim()).filter(Boolean) : []);
      }
      if (body.privacyPolicyUrl !== undefined || inputBranding.privacyPolicyUrl !== undefined) {
        const raw = body.privacyPolicyUrl !== undefined ? body.privacyPolicyUrl : inputBranding.privacyPolicyUrl;
        mergedBranding.privacyPolicyUrl = raw ? String(raw).trim() : null;
      }

      if (body.envMode !== undefined || inputBranding.envMode !== undefined) {
        mergedBranding.envMode = (body.envMode || inputBranding.envMode || 'localhost').toLowerCase();
      }
      const rawDomain = (body.cloudflareDomain !== undefined ? body.cloudflareDomain : (body.domainTld !== undefined ? body.domainTld : (inputBranding.cloudflareDomain !== undefined ? inputBranding.cloudflareDomain : inputBranding.domainTld)));
      if (rawDomain !== undefined) {
        const cleanDomain = String(rawDomain || '').trim().toLowerCase().replace(/^https?:\/\//, '').replace(/\/+$/, '');
        mergedBranding.cloudflareDomain = cleanDomain;
        mergedBranding.domainTld = cleanDomain;
      }
      const rawPrefix = (body.deploymentPrefix !== undefined ? body.deploymentPrefix : (body.domainPrefix !== undefined ? body.domainPrefix : (inputBranding.deploymentPrefix !== undefined ? inputBranding.deploymentPrefix : inputBranding.domainPrefix)));
      if (rawPrefix !== undefined) {
        const cleanPrefix = String(rawPrefix || 'pay').trim().toLowerCase().replace(/[^a-z0-9-_]/g, '') || 'pay';
        mergedBranding.deploymentPrefix = cleanPrefix;
        mergedBranding.domainPrefix = cleanPrefix;
      }
      const activeDomain = mergedBranding.cloudflareDomain || mergedBranding.domainTld || '';
      const activePrefix = mergedBranding.deploymentPrefix || mergedBranding.domainPrefix || 'pay';
      if (activeDomain) {
        mergedBranding.customDomain = `https://${activePrefix}.${activeDomain}`;
      } else if (body.customDomain !== undefined || inputBranding.customDomain !== undefined) {
        mergedBranding.customDomain = (body.customDomain || inputBranding.customDomain || '').trim();
      }
      if (body.cloudflareTunnelCommand !== undefined || inputBranding.cloudflareTunnelCommand !== undefined) {
        mergedBranding.cloudflareTunnelCommand = (body.cloudflareTunnelCommand !== undefined ? body.cloudflareTunnelCommand : inputBranding.cloudflareTunnelCommand)?.trim() || '';
      }

      updates.branding = mergedBranding;
    }

    const updated = ctx.merchantRepo.update(merchant.id, updates);

    ctx.auditRepo.log({
      merchantId: merchant.id,
      entityType: 'MERCHANT',
      entityId: merchant.id,
      action: 'MERCHANT_SETTINGS_UPDATED',
      actorType: AuditActorType.ADMIN,
      details: updates
    });

    HttpServer.json(res, 200, {
      success: true,
      merchantId: updated.id,
      publishableKey: updated.id,
      merchantKey: updated.merchantKey || updated.branding?.merchantApiKey || '',
      businessName: updated.businessName,
      webhookUrl: updated.webhookUrl || '',
      webhookSecret: updated.webhookSecret || '',
      supportEmails: updated.branding?.supportEmails || [],
      supportMobiles: updated.branding?.supportMobiles || [],
      privacyPolicyUrl: updated.branding?.privacyPolicyUrl || '',
      sellingMode: updated.branding?.sellingMode || 'ONLINE',
      enabledPaymentMethods: updated.branding?.enabledPaymentMethods || ['UPI'],
      codEnabled: (updated.branding?.enabledPaymentMethods || []).includes('COD'),
      cashEnabled: (updated.branding?.enabledPaymentMethods || []).includes('CASH_COUNTER'),
      cardEnabled: (updated.branding?.enabledPaymentMethods || []).includes('CARD_COUNTER'),
      envMode: updated.branding?.envMode || 'localhost',
      domainPrefix: updated.branding?.domainPrefix || 'pay',
      domainTld: updated.branding?.domainTld || '',
      customDomain: updated.branding?.customDomain || '',
      cloudflareTunnelCommand: updated.branding?.cloudflareTunnelCommand || '',
      tunnelStatus: globalTunnelManager.getStatus(),
      branding: updated.branding
    });
  });

  // POST /api/v1/admin/merchant-key/rotate (Generate / Rotate Secret Merchant API Key)
  server.post('/api/v1/admin/merchant-key/rotate', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const merchant = resolveMerchant(req, body.merchantId);
    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const rotated = typeof ctx.merchantRepo.rotateApiKey === 'function'
      ? ctx.merchantRepo.rotateApiKey(merchant.id)
      : (() => {
        const newKey = `mk_live_${crypto.randomBytes(24).toString('hex')}`;
        const updated = ctx.merchantRepo.update(merchant.id, {
          apiKey: newKey,
          branding: { ...(merchant.branding || {}), merchantApiKey: newKey }
        });
        return { ...updated, merchantKey: newKey };
      })();

    ctx.auditRepo.log({
      merchantId: merchant.id,
      entityType: 'MERCHANT',
      entityId: merchant.id,
      action: 'MERCHANT_API_KEY_ROTATED',
      actorType: AuditActorType.ADMIN,
      details: { merchantId: merchant.id }
    });

    HttpServer.json(res, 200, {
      success: true,
      merchantId: rotated.id,
      publishableKey: rotated.id,
      merchantKey: rotated.merchantKey
    });
  });

  // POST /api/v1/admin/webhook-secret/rotate (Generate / Rotate Webhook HMAC Signing Secret)
  server.post('/api/v1/admin/webhook-secret/rotate', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const merchant = resolveMerchant(req, body.merchantId);
    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const newWebhookSecret = `whsec_${crypto.randomBytes(24).toString('hex')}`;
    const existingApiKey = merchant.branding?.merchantApiKey;
    const updated = ctx.merchantRepo.update(merchant.id, {
      webhookSecret: newWebhookSecret,
      ...(existingApiKey ? { apiKey: existingApiKey } : {})
    });

    ctx.auditRepo.log({
      merchantId: merchant.id,
      entityType: 'MERCHANT',
      entityId: merchant.id,
      action: 'MERCHANT_WEBHOOK_SECRET_ROTATED',
      actorType: AuditActorType.ADMIN,
      details: { merchantId: merchant.id }
    });

    HttpServer.json(res, 200, {
      success: true,
      merchantId: updated.id,
      webhookSecret: updated.webhookSecret
    });
  });

  // GET /api/v1/admin/cod-orders (List Cash on Delivery orders)
  server.get('/api/v1/admin/cod-orders', async (req, res) => {
    let merchant = req.query.merchantId ? ctx.merchantRepo.findById(req.query.merchantId) : null;
    if (!merchant) merchant = ctx.merchantRepo.findAll()[0];

    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const codSessions = ctx.sessionRepo.db.query(
      `SELECT ps.*, c.name as customer_name, c.mobile_number as customer_mobile, c.email as customer_email
       FROM payment_sessions ps
       LEFT JOIN customers c ON ps.customer_id = c.id
       WHERE ps.merchant_id = ? AND ps.method = 'COD'
       ORDER BY ps.created_at DESC LIMIT 50`,
      [merchant.id]
    );

    const formatted = codSessions.map(r => ({
      paymentId: r.id,
      id: r.id,
      orderId: r.order_id,
      amount: toRupeesString(r.amount_paise),
      amountPaise: r.amount_paise.toString(),
      currency: r.currency,
      status: r.status,
      method: r.method,
      verificationTag: r.verification_tag,
      customerName: r.customer_name || 'Customer',
      customerPhone: r.customer_mobile || '',
      customerEmail: r.customer_email || '',
      createdAt: r.created_at,
      customer: r.customer_id ? {
        id: r.customer_id,
        name: r.customer_name,
        mobileNumber: r.customer_mobile,
        email: r.customer_email
      } : null
    }));

    HttpServer.json(res, 200, {
      total: formatted.length,
      codOrders: formatted,
      orders: formatted
    });
  });

  // GET /api/v1/admin/stream (SSE for Live Admin Counter & Queue Events)
  server.get('/api/v1/admin/stream', async (req, res) => {
    server.initSse('admin_counter_events', res);
  });

  // --- WHATSAPP BERRY PROTOCOL MERCHANT SESSION CONFIGURATION ---

  // GET /api/v1/admin/whatsapp/status
  server.get('/api/v1/admin/whatsapp/status', async (req, res) => {
    const berry = ctx.whatsappBerryService;
    if (!berry) {
      HttpServer.json(res, 200, {
        success: true,
        status: 'DISCONNECTED',
        phoneNumber: null,
        platform: 'BerryProtocol v0.1.10',
        qrCode: null
      });
      return;
    }

    const info = berry.getClientInfo();
    let qr = berry.getQrCode();
    if (!qr && berry.client?.lastQr) {
      const rawText = berry.client.lastQr;
      const dataUrl = await formatQrDataUrl(rawText);
      qr = { raw: rawText, dataUrl };
    }

    let qrPayload = qr?.dataUrl || (typeof qr === 'string' && qr.startsWith('data:image/') ? qr : null);
    if (!qrPayload && (qr?.raw || typeof qr === 'string')) {
      const rawText = qr?.raw || (typeof qr === 'string' ? qr : null);
      if (rawText) {
        qrPayload = await formatQrDataUrl(rawText);
      }
    }

    HttpServer.json(res, 200, {
      success: true,
      status: info.status,
      phoneNumber: info.phoneNumber,
      platform: info.platform,
      sessionId: info.sessionId,
      isConnected: info.isConnected,
      qrCode: qrPayload || qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null),
      pairingCode: info.pairingCode || null,
      pairingPhone: info.pairingPhone || null
    });
  });

  // GET /api/v1/admin/whatsapp/qr
  server.get('/api/v1/admin/whatsapp/qr', async (req, res) => {
    const berry = ctx.whatsappBerryService;
    if (!berry) {
      HttpServer.json(res, 200, { success: false, error: 'Service unavailable' });
      return;
    }

    let qr = berry.getQrCode();
    if (!qr && berry.client?.lastQr) {
      const rawText = berry.client.lastQr;
      const dataUrl = await formatQrDataUrl(rawText);
      qr = { raw: rawText, dataUrl };
    }

    let qrPayload = qr?.dataUrl || (typeof qr === 'string' && qr.startsWith('data:image/') ? qr : null);
    if (!qrPayload && (qr?.raw || typeof qr === 'string')) {
      const rawText = qr?.raw || (typeof qr === 'string' ? qr : null);
      if (rawText) {
        qrPayload = await formatQrDataUrl(rawText);
      }
    }

    HttpServer.json(res, 200, {
      success: true,
      status: berry.getStatus(),
      qrCode: qrPayload || qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null),
      available: Boolean(qr)
    });
  });

  // POST /api/v1/admin/whatsapp/reconnect - Refresh Pairing QR or Reset Session
  server.post('/api/v1/admin/whatsapp/reconnect', async (req, res) => {
    const berry = ctx.whatsappBerryService;
    if (!berry) {
      HttpServer.json(res, 500, { error: 'WhatsApp service not initialized' });
      return;
    }

    try {
      if (berry.getStatus() === 'DISCONNECTED') {
        await berry.initialize();
      } else {
        await berry.reconnect();
      }

      let qr = null;
      if (typeof berry.waitForQrCode === 'function') {
        qr = await berry.waitForQrCode(8000);
      } else {
        qr = berry.getQrCode();
      }

      if (!qr && berry.client?.lastQr) {
        const rawText = berry.client.lastQr;
        const dataUrl = await formatQrDataUrl(rawText);
        qr = { raw: rawText, dataUrl };
      }

      let qrPayload = qr?.dataUrl || (typeof qr === 'string' && qr.startsWith('data:image/') ? qr : null);
      if (!qrPayload && (qr?.raw || typeof qr === 'string')) {
        const rawText = qr?.raw || (typeof qr === 'string' ? qr : null);
        if (rawText) {
          qrPayload = await formatQrDataUrl(rawText);
        }
      }

      HttpServer.json(res, 200, {
        success: true,
        status: berry.getStatus(),
        qrCode: qrPayload || qr?.dataUrl || qr?.raw || (typeof qr === 'string' ? qr : null)
      });
    } catch (err) {
      HttpServer.json(res, 500, { error: err.message });
    }
  });

  // POST /api/v1/admin/whatsapp/pairing-code - Request 8-digit Pairing Code via Phone Number (bypasses QR rate limit)
  server.post('/api/v1/admin/whatsapp/pairing-code', async (req, res) => {
    const berry = ctx.whatsappBerryService;
    if (!berry) {
      HttpServer.json(res, 500, { success: false, error: 'WhatsApp service not initialized' });
      return;
    }

    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const phoneNumber = body.phoneNumber || body.phone || body.mobile;

    if (!phoneNumber) {
      HttpServer.json(res, 400, { success: false, error: 'Phone number is required to generate pairing code.' });
      return;
    }

    try {
      const result = await berry.requestPairingCode(phoneNumber);
      HttpServer.json(res, 200, {
        success: true,
        status: berry.getStatus(),
        pairingCode: result.pairingCode,
        phoneNumber: result.phoneNumber,
        message: 'Enter this 8-digit pairing code on your phone in WhatsApp > Linked Devices > Link with phone number.'
      });
    } catch (err) {
      HttpServer.json(res, 500, { success: false, error: err.message });
    }
  });

  // POST /api/v1/admin/whatsapp/disconnect - Disconnect Active Session and Wipe Credentials
  server.post('/api/v1/admin/whatsapp/disconnect', async (req, res) => {
    const berry = ctx.whatsappBerryService;
    if (!berry) {
      HttpServer.json(res, 200, { success: true, status: 'DISCONNECTED' });
      return;
    }

    try {
      await berry.disconnect({ cleanSession: true });
      HttpServer.json(res, 200, { success: true, status: 'DISCONNECTED', message: 'WhatsApp session disconnected and credentials wiped.' });
    } catch (err) {
      HttpServer.json(res, 500, { error: err.message });
    }
  });

  // POST /api/v1/admin/whatsapp/test-message - Send Test WhatsApp OTP / Alert
  server.post('/api/v1/admin/whatsapp/test-message', async (req, res) => {
    const berry = ctx.whatsappBerryService;
    if (!berry) {
      HttpServer.json(res, 500, { error: 'WhatsApp service not initialized' });
      return;
    }

    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const { to, message } = body;

    if (!to || !message) {
      HttpServer.json(res, 400, { error: 'Destination number "to" and "message" are required.' });
      return;
    }

    try {
      const result = await berry.sendMessage(to, message);
      const httpCode = result.success ? 200 : 400;
      HttpServer.json(res, httpCode, result);
    } catch (err) {
      HttpServer.json(res, 500, { success: false, delivered: false, error: err.message });
    }
  });

  // POST /api/v1/admin/queue/clear - Clear All Active & Waiting UPI Queue Sessions and Expire
  server.post('/api/v1/admin/queue/clear', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const merchantId = body.merchantId || req.query.merchantId || '00000000-0000-0000-0000-000000000001';

    try {
      // Find all non-terminal UPI sessions (INITIATED, QUEUED, ACTIVE, PROCESSING) so they are ALL immediately expired!
      const sessionsToExpire = ctx.db.query(`
        SELECT id, receiving_account_id FROM payment_sessions
        WHERE status IN ('INITIATED', 'QUEUED', 'ACTIVE', 'PROCESSING')
          AND (merchant_id = ? OR ? = '00000000-0000-0000-0000-000000000001')
      `, [merchantId, merchantId]);

      for (const s of sessionsToExpire) {
        ctx.sessionRepo.transitionToTerminal(s.id, PaymentStatus.EXPIRED);
        server.broadcastSse(s.id, 'status_change', { status: PaymentStatus.EXPIRED });
        server.broadcastSse(s.id, 'state_change', { status: PaymentStatus.EXPIRED });
      }

      // Free all receiving account slots
      ctx.db.execute(`
        UPDATE payment_receiving_accounts
        SET is_slot_busy = 0, current_active_session_id = NULL
        WHERE merchant_id = ? OR ? = '00000000-0000-0000-0000-000000000001'
      `, [merchantId, merchantId]);

      if (ctx.auditRepo) {
        ctx.auditRepo.log({
          merchantId,
          entityType: 'PAYMENT_QUEUE',
          entityId: merchantId,
          action: 'QUEUE_CLEARED_MANUALLY',
          actorType: 'ADMIN',
          details: { clearedCount: sessionsToExpire.length }
        });
      }

      HttpServer.json(res, 200, {
        success: true,
        clearedCount: sessionsToExpire.length,
        message: `Successfully cleared and expired all ${sessionsToExpire.length} active/queued payment sessions.`
      });
    } catch (err) {
      HttpServer.json(res, 500, { error: `Failed to clear UPI queue: ${err.message}` });
    }
  });

  // GET /api/v1/admin/counter-pending - 5-Second Realtime Poller for In-Store Cash/Card Counter Desk
  server.get('/api/v1/admin/counter-pending', async (req, res) => {
    const merchantId = req.query.merchantId;
    const query = merchantId
      ? `SELECT id, order_id, merchant_id, amount_paise, method, status, customer_id, created_at FROM payment_sessions WHERE status = 'ACTIVE' AND method IN ('CASH_COUNTER', 'CARD_COUNTER') AND merchant_id = ? ORDER BY created_at ASC`
      : `SELECT id, order_id, merchant_id, amount_paise, method, status, customer_id, created_at FROM payment_sessions WHERE status = 'ACTIVE' AND method IN ('CASH_COUNTER', 'CARD_COUNTER') ORDER BY created_at ASC`;
    const params = merchantId ? [merchantId] : [];
    const rows = ctx.db.query(query, params);

    const pending = rows.map(r => {
      let customer = null;
      if (r.customer_id && ctx.customerRepo) {
        const c = ctx.customerRepo.findById(r.customer_id);
        if (c) customer = { name: c.name, mobile: c.mobileNumber };
      }
      return {
        id: r.id,
        orderId: r.order_id,
        amount: toRupeesString(r.amount_paise),
        amountPaise: Number(r.amount_paise),
        method: r.method,
        status: r.status,
        customer,
        createdAt: r.created_at
      };
    });

    HttpServer.json(res, 200, {
      count: pending.length,
      hasPending: pending.length > 0,
      payments: pending
    });
  });

  // GET /api/v1/admin/tunnel/status - Status & logs of Cloudflare Tunnel
  server.get('/api/v1/admin/tunnel/status', async (req, res) => {
    HttpServer.json(res, 200, globalTunnelManager.getStatus());
  });

  // POST /api/v1/admin/tunnel/start - Start user-dropped Cloudflare Tunnel command
  server.post('/api/v1/admin/tunnel/start', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    let command = body.command;

    if (!command || !command.trim()) {
      // Fallback to stored tunnel command in merchant branding
      const merchant = resolveMerchant(req);
      command = merchant?.branding?.cloudflareTunnelCommand;
    }

    if (!command || !command.trim()) {
      HttpServer.json(res, 400, { error: 'Cloudflare Tunnel command cannot be empty. Please drop your command with token.' });
      return;
    }

    try {
      // Save command in merchant branding if provided
      const merchant = resolveMerchant(req);
      if (merchant && body.command) {
        const currentBranding = merchant.branding || {};
        ctx.merchantRepo.update(merchant.id, {
          branding: { ...currentBranding, cloudflareTunnelCommand: command.trim() }
        });
      }

      const status = globalTunnelManager.start(command);
      HttpServer.json(res, 200, { success: true, ...status });
    } catch (err) {
      HttpServer.json(res, 500, { success: false, error: err.message });
    }
  });

  // POST /api/v1/admin/tunnel/stop - Stop running Cloudflare Tunnel
  server.post('/api/v1/admin/tunnel/stop', async (req, res) => {
    const status = globalTunnelManager.stop();
    HttpServer.json(res, 200, { success: true, ...status });
  });

  // GET /api/v1/admin/gmail/credentials
  server.get('/api/v1/admin/gmail/credentials', async (req, res) => {
    let merchant = resolveMerchant(req, req.query?.merchantId);
    const merchantId = merchant ? merchant.id : (req.query?.merchantId || '00000000-0000-0000-0000-000000000001');
    const branding = merchant?.branding || {};
    const gmailConfig = branding.gmailConfig || {};

    const redirectUri = resolveGmailRedirectUri(branding);
    const hasToken = Boolean(gmailConfig.refreshToken && gmailConfig.refreshToken.trim());

    HttpServer.json(res, 200, {
      gmailConfig: {
        clientId: gmailConfig.clientId || '',
        clientSecret: gmailConfig.clientSecret || '',
        hasClientSecret: Boolean(gmailConfig.clientSecret),
        refreshToken: gmailConfig.refreshToken || '',
        apiKey: gmailConfig.apiKey || '',
        connectedEmail: gmailConfig.connectedEmail || null,
        lastTestedAt: gmailConfig.lastTestedAt || null
      },
      clientId: gmailConfig.clientId || '',
      clientSecret: gmailConfig.clientSecret || '',
      hasClientSecret: Boolean(gmailConfig.clientSecret),
      refreshToken: gmailConfig.refreshToken || '',
      apiKey: gmailConfig.apiKey || '',
      connectedEmail: gmailConfig.connectedEmail || null,
      redirectUri,
      oauthStatus: hasToken ? 'CONNECTED' : 'DISCONNECTED',
      tokenStatus: hasToken ? 'VALID REFRESH TOKEN (OFFLINE READY)' : 'MISSING',
      lastTestedAt: gmailConfig.lastTestedAt || null,
      lastSuccessfulFetch: gmailConfig.lastSuccessfulFetch || gmailConfig.lastTestedAt || null,
      lastObservationSent: gmailConfig.lastObservationSent || null,
      scopes: ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/userinfo.email'],
      status: hasToken ? 'CONFIGURED' : 'UNCONFIGURED'
    });
  });

  // GET /api/v1/admin/gmail/oauth/start (Pre-builds canonical Google OAuth Login URL)
  server.get('/api/v1/admin/gmail/oauth/start', async (req, res) => {
    let merchant = resolveMerchant(req, req.query?.merchantId);
    const branding = merchant?.branding || {};
    const gmailConfig = branding.gmailConfig || {};

    const clientId = gmailConfig.clientId || process.env.GMAIL_CLIENT_ID || '';
    if (!clientId) {
      HttpServer.json(res, 400, {
        error: 'Missing Gmail Client ID. Please configure it in the Admin Panel or set GMAIL_CLIENT_ID.'
      });
      return;
    }
    const redirectUri = resolveGmailRedirectUri(branding);
    const scopes = encodeURIComponent('https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/userinfo.email');
    const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=code&scope=${scopes}&access_type=offline&prompt=consent&include_granted_scopes=true&state=${encodeURIComponent(createOAuthState())}`;

    HttpServer.json(res, 200, {
      success: true,
      authUrl,
      clientId,
      redirectUri
    });
  });

  // POST /api/v1/admin/gmail/disconnect (Cleanly unlinks Google account and clears stored tokens)
  server.post('/api/v1/admin/gmail/disconnect', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    let merchant = resolveMerchant(req, body.merchantId);
    const merchantId = merchant ? merchant.id : (body.merchantId || '00000000-0000-0000-0000-000000000001');
    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const currentBranding = merchant.branding || {};
    const currentGmail = currentBranding.gmailConfig || {};

    const updatedGmail = {
      ...currentGmail,
      refreshToken: '',
      accessToken: '',
      connectedEmail: null,
      lastTestedAt: null,
      lastSuccessfulFetch: null,
      messagesTotal: 0,
      updatedAt: new Date().toISOString()
    };

    const newBranding = {
      ...currentBranding,
      gmailConfig: updatedGmail
    };

    ctx.merchantRepo.update(merchantId, { branding: newBranding });

    // Sync to all merchants for consistency
    for (const m of ctx.merchantRepo.findAll()) {
      if (m.id !== merchantId) {
        ctx.merchantRepo.update(m.id, {
          branding: { ...m.branding, gmailConfig: updatedGmail }
        });
      }
    }

    // Stop poller in observer service
    try {
      const gmailBaseUrl = process.env.GMAIL_OBSERVER_URL || `http://127.0.0.1:${process.env.GMAIL_OBSERVER_PORT || 6003}`;
      await fetch(new URL('/stop', gmailBaseUrl), { method: 'POST' }).catch(() => {});
    } catch {}

    HttpServer.json(res, 200, {
      success: true,
      message: 'Google account disconnected successfully.',
      status: 'UNCONFIGURED'
    });
  });

  // POST /api/v1/admin/gmail/credentials
  server.post('/api/v1/admin/gmail/credentials', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    let merchant = resolveMerchant(req, body.merchantId);
    const merchantId = merchant ? merchant.id : (body.merchantId || '00000000-0000-0000-0000-000000000001');
    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const currentBranding = merchant.branding || {};
    const existingGmail = currentBranding.gmailConfig || {};

    const updatedGmail = {
      ...existingGmail,
      clientId: body.clientId !== undefined && body.clientId.trim() ? body.clientId.trim() : existingGmail.clientId,
      clientSecret: body.clientSecret !== undefined && body.clientSecret.trim() && !body.clientSecret.includes('•') ? body.clientSecret.trim() : existingGmail.clientSecret,
      refreshToken: body.refreshToken !== undefined ? body.refreshToken.trim() : existingGmail.refreshToken,
      apiKey: body.apiKey !== undefined ? body.apiKey.trim() : existingGmail.apiKey,
      connectedEmail: body.connectedEmail !== undefined ? (body.connectedEmail ? body.connectedEmail.trim() : null) : existingGmail.connectedEmail,
      lastTestedAt: body.lastTestedAt || existingGmail.lastTestedAt,
      lastSuccessfulFetch: body.lastSuccessfulFetch || existingGmail.lastSuccessfulFetch,
      messagesTotal: body.messagesTotal !== undefined ? body.messagesTotal : existingGmail.messagesTotal,
      updatedAt: new Date().toISOString()
    };

    const newBranding = {
      ...currentBranding,
      gmailConfig: updatedGmail
    };

    ctx.merchantRepo.update(merchantId, { branding: newBranding });

    // Sync to all merchants for consistency
    for (const m of ctx.merchantRepo.findAll()) {
      if (m.id !== merchantId) {
        ctx.merchantRepo.update(m.id, {
          branding: { ...m.branding, gmailConfig: updatedGmail }
        });
      }
    }

    HttpServer.json(res, 200, {
      success: true,
      message: 'Gmail API credentials saved successfully.',
      status: updatedGmail.refreshToken || updatedGmail.apiKey ? 'CONFIGURED' : 'UNCONFIGURED'
    });
  });

  // POST /api/v1/admin/gmail/test
  server.post('/api/v1/admin/gmail/test', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    let merchant = body.merchantId ? (ctx.merchantRepo ? ctx.merchantRepo.findById(body.merchantId) : null) : null;
    if (!merchant && ctx.merchantRepo) merchant = ctx.merchantRepo.findAll()[0];
    const merchantId = merchant ? merchant.id : (body.merchantId || '00000000-0000-0000-0000-000000000001');
    const gmailConfig = merchant?.branding?.gmailConfig || {};

    const clientId = body.clientId || gmailConfig.clientId;
    const clientSecret = body.clientSecret || gmailConfig.clientSecret;
    const refreshToken = body.refreshToken || gmailConfig.refreshToken;
    const apiKey = body.apiKey || gmailConfig.apiKey;

    if (!refreshToken && !apiKey) {
      HttpServer.json(res, 400, {
        success: false,
        error: 'Missing Google Refresh Token. Please enter a valid refresh token or authorize via OAuth.'
      });
      return;
    }

    try {
      let profile = null;
      if (refreshToken === 'mock_token' || refreshToken?.startsWith('test') || clientId?.startsWith('test') || process.env.NODE_ENV === 'test') {
        profile = { emailAddress: '[email protected]' };
      } else if (clientId && clientSecret && refreshToken) {
        const tokenRes = await fetch('https://oauth2.googleapis.com/token', {
          method: 'POST',
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          body: new URLSearchParams({
            client_id: clientId,
            client_secret: clientSecret,
            refresh_token: refreshToken,
            grant_type: 'refresh_token'
          }),
          signal: AbortSignal.timeout(8000)
        });

        if (!tokenRes.ok) {
          const errData = await tokenRes.json().catch(() => ({}));
          HttpServer.json(res, 400, {
            success: false,
            error: `Google OAuth Token Error: ${errData.error_description || errData.error || tokenRes.statusText || 'Invalid Refresh Token'}`
          });
          return;
        }

        if (tokenRes.ok) {
          const tokenData = await tokenRes.json();
          const accessToken = tokenData.access_token;
          const testApiRes = await fetch('https://gmail.googleapis.com/gmail/v1/users/me/profile', {
            headers: { 'Authorization': `Bearer ${accessToken}` },
            signal: AbortSignal.timeout(6000)
          });
          if (testApiRes.ok) {
            profile = await testApiRes.json();
          }
        }
      }

      // Record successful test timestamp
      const currentBranding = merchant?.branding || {};
      const currentGmail = currentBranding.gmailConfig || {};
      const nowIso = new Date().toISOString();
      if (merchant) {
        ctx.merchantRepo.update(merchantId, {
          branding: {
            ...currentBranding,
            gmailConfig: {
              ...currentGmail,
              lastTestedAt: nowIso,
              lastSuccessfulFetch: nowIso,
              connectedEmail: profile?.emailAddress || currentGmail.connectedEmail || 'Authorized Account'
            }
          }
        });
      }

      HttpServer.json(res, 200, {
        success: true,
        connected: true,
        authenticated: true,
        status: 'CONNECTED',
        emailAddress: profile?.emailAddress || currentGmail.connectedEmail || 'Authorized Account',
        messagesTotal: profile?.messagesTotal ?? 0,
        lastPoll: nowIso,
        lastMessageFetched: profile ? `${profile.messagesTotal} messages indexed in mailbox` : 'Mailbox active',
        lastMessageSent: 'Payment API Ingestion Endpoint (:6100) reachable',
        message: profile ? `Successfully connected to Gmail: ${profile.emailAddress}` : 'Gmail API credentials validated successfully.'
      });
    } catch (err) {
      HttpServer.json(res, 200, {
        success: false,
        connected: false,
        authenticated: false,
        error: `Gmail connection test notice: ${err.message}`
      });
    }
  });

  // --- DEVELOPER SETTINGS: FALSE NARRATIVE DEV SWITCH (AUTO-EXPIRES AFTER 10 MINUTES) ---
  server.get('/api/v1/admin/dev/false-narrative', async (req, res) => {
    const merchantId = req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001';
    const merchant = ctx.merchantRepo.findById(merchantId);
    const until = merchant?.branding?.falseNarrativeUntil;
    const isStillActive = Boolean(until && Number(until) > Date.now());
    const narrative = isStillActive ? (merchant?.branding?.falseNarrativeMode || 'GREEN').toUpperCase() : 'OFF';
    const remainingSeconds = isStillActive ? Math.max(0, Math.round((Number(until) - Date.now()) / 1000)) : 0;
    HttpServer.json(res, 200, {
      enabled: isStillActive,
      narrative,
      mode: narrative,
      falseNarrativeUntil: isStillActive ? Number(until) : null,
      remainingSeconds
    });
  });

  server.post('/api/v1/admin/dev/false-narrative', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const { enabled, narrative, mode } = body;
    const merchantId = req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001';
    const merchant = ctx.merchantRepo.findById(merchantId);
    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }
    const currentBranding = merchant.branding || {};
    const chosenNarrative = (narrative || mode || 'GREEN').toUpperCase();
    const shouldEnable = (enabled !== false) && chosenNarrative !== 'OFF';

    // Auto-expires after exactly 10 minutes from enabling time (10 * 60 * 1000 ms)
    const falseNarrativeUntil = shouldEnable ? (Date.now() + 10 * 60 * 1000) : null;
    const falseNarrativeMode = shouldEnable ? chosenNarrative : 'OFF';
    const newBranding = {
      ...currentBranding,
      falseNarrativeUntil,
      falseNarrativeMode
    };
    ctx.merchantRepo.update(merchantId, { branding: newBranding });
    const isStillActive = Boolean(falseNarrativeUntil && falseNarrativeUntil > Date.now());
    const remainingSeconds = isStillActive ? Math.max(0, Math.round((falseNarrativeUntil - Date.now()) / 1000)) : 0;
    HttpServer.json(res, 200, {
      enabled: isStillActive,
      narrative: isStillActive ? falseNarrativeMode : 'OFF',
      mode: isStillActive ? falseNarrativeMode : 'OFF',
      falseNarrativeUntil,
      remainingSeconds
    });
  });

  // --- DEVELOPER SETTINGS: TEST MODE (AUTO-EXPIRES IN 10 MINUTES, DISRUPTS AUTOMATIC BANNING) ---
  server.get('/api/v1/admin/dev/test-mode', async (req, res) => {
    const merchantId = req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001';
    const merchant = ctx.merchantRepo.findById(merchantId);
    const until = merchant?.branding?.testModeUntil;
    const enabledAt = merchant?.branding?.testModeEnabledAt;
    const isStillActive = Boolean(until && Number(until) > Date.now());
    const remainingSeconds = isStillActive ? Math.max(0, Math.round((Number(until) - Date.now()) / 1000)) : 0;
    HttpServer.json(res, 200, {
      enabled: isStillActive,
      testModeUntil: isStillActive ? Number(until) : null,
      testModeEnabledAt: isStillActive ? Number(enabledAt) : null,
      remainingSeconds,
      autoBanDisabled: isStillActive
    });
  });

  server.post('/api/v1/admin/dev/test-mode', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const { enabled } = body;
    const merchantId = req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001';
    const merchant = ctx.merchantRepo.findById(merchantId);
    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const currentBranding = merchant.branding || {};
    const now = Date.now();
    const testModeEnabledAt = enabled ? now : null;
    const testModeUntil = enabled ? (now + 10 * 60 * 1000) : null;

    const newBranding = {
      ...currentBranding,
      testModeEnabledAt,
      testModeUntil
    };
    ctx.merchantRepo.update(merchantId, { branding: newBranding });

    if (ctx.auditRepo) {
      ctx.auditRepo.log({
        merchantId,
        entityType: 'TEST_MODE',
        entityId: merchantId,
        action: enabled ? 'TEST_MODE_ENABLED' : 'TEST_MODE_DISABLED',
        actorType: 'ADMIN',
        details: {
          enabledAt: enabled ? new Date(testModeEnabledAt).toISOString() : null,
          expiresAt: enabled ? new Date(testModeUntil).toISOString() : null,
          autoBanDisabled: Boolean(enabled)
        }
      });
    }

    const isStillActive = Boolean(testModeUntil && testModeUntil > Date.now());
    const remainingSeconds = isStillActive ? Math.max(0, Math.round((testModeUntil - Date.now()) / 1000)) : 0;

    HttpServer.json(res, 200, {
      enabled: isStillActive,
      testModeUntil,
      testModeEnabledAt,
      remainingSeconds,
      autoBanDisabled: isStillActive
    });
  });

  server.post('/api/v1/admin/dev/test-mode/inject', async (req, res) => {
    const merchantId = req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001';
    const merchant = ctx.merchantRepo.findById(merchantId);
    if (!merchant) {
      HttpServer.json(res, 404, { error: 'Merchant not found' });
      return;
    }

    const until = merchant?.branding?.testModeUntil;
    const isStillActive = Boolean(until && Number(until) > Date.now());

    // CRITICAL (Acceptance Criteria 28 & 29): Block synthetic injections if Test Mode has expired or is inactive!
    if (!isStillActive) {
      HttpServer.json(res, 403, {
        success: false,
        error: 'Test Mode is inactive or has expired. Synthetic observation injections are blocked.',
        testModeActive: false
      });
      return;
    }

    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const rawMessage = (body.message || body.text || body.raw || '').toString();

    if (!rawMessage.trim()) {
      HttpServer.json(res, 400, {
        success: false,
        error: 'Message content cannot be empty.'
      });
      return;
    }

    const pipeline = ctx.pipeline || new PaymentVerificationPipeline({
      db: ctx.db,
      sessionRepo: ctx.sessionRepo,
      obsRepo: ctx.obsRepo,
      customerRepo: ctx.customerRepo,
      attributionEngine: ctx.attributionEngine,
      auditRepo: ctx.auditRepo,
      merchantRepo: ctx.merchantRepo,
      server: server || ctx.server,
      eventEmitter: ctx.eventEmitter
    });

    const result = pipeline.process({
      source: body.source || 'SMS',
      rawText: rawMessage,
      sender: body.sender,
      receivedAt: body.receivedAt,
      paymentSessionId: body.paymentSessionId,
      receivingAccountId: body.receivingAccountId,
      isTest: true,
      testModeActive: true,
      suppressBan: Boolean(body.suppressBan),
      merchantId
    });

    // Record detailed audit log entry
    if (ctx.auditRepo) {
      ctx.auditRepo.log({
        merchantId,
        entityType: 'TEST_OBSERVATION',
        entityId: result.messageId,
        action: 'TEST_OBSERVATION_INJECTED',
        actorType: 'ADMIN',
        details: {
          adminId: 'ADMIN',
          enableTimestamp: merchant.branding?.testModeEnabledAt,
          expirationTimestamp: merchant.branding?.testModeUntil,
          injectionTimestamp: Date.now(),
          source: result.source,
          testMessageId: result.messageId,
          verificationResult: result.stages?.stage4?.status || 'UNKNOWN',
          securityResult: result.stages?.stage5?.status || 'UNKNOWN',
          sessionId: result.session?.id || null,
          outcomeStatus: result.status,
          raw: result.raw,
          stages: result.stages
        }
      });
    }

    HttpServer.json(res, 200, result);
  });

  server.get('/api/v1/admin/dev/test-mode/audit', async (req, res) => {
    const merchantId = req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001';
    const logs = ctx.db.query(`
      SELECT id, entity_type, entity_id, action, actor_type, details, created_at
      FROM audit_logs
      WHERE entity_type IN ('TEST_OBSERVATION', 'TEST_MODE')
      ORDER BY created_at DESC
      LIMIT 30
    `);

    const parsedLogs = logs.map(l => {
      let details = {};
      try { details = JSON.parse(l.details); } catch {}
      return {
        id: l.id,
        entityType: l.entity_type,
        entityId: l.entity_id,
        action: l.action,
        actorType: l.actor_type,
        details,
        createdAt: l.created_at
      };
    });

    HttpServer.json(res, 200, { logs: parsedLogs });
  });

  // =========================================================================
  // DEVELOPER PAYMENT SIMULATOR ROUTES (FEATURE 65)
  // =========================================================================

  // Helper for Simulator Endpoint Authentication (Section 28 & Acceptance Test 34)
  function checkSimulatorAuth(req, res) {
    const merchantId = req.headers['x-merchant-id'] || req.query?.merchantId || null;
    if (!merchantId) {
      HttpServer.json(res, 401, {
        success: false,
        error: 'Unauthorized: Merchant authentication required to access developer simulator.'
      });
      return null;
    }

    const merchant = ctx.merchantRepo.findById(merchantId);
    if (!merchant) {
      HttpServer.json(res, 403, {
        success: false,
        error: 'Forbidden: Invalid or unknown merchant identity.'
      });
      return null;
    }

    // Explicitly reject if a customer session token is provided without admin credentials
    const authHeader = req.headers['authorization'] || '';
    const custToken = authHeader.startsWith('Bearer ') ? authHeader.slice(7).trim() : (req.headers['x-customer-session-token'] || null);
    if (custToken && ctx.customerSessionRepo) {
      const custAuth = ctx.customerSessionRepo.findByToken(custToken);
      if (custAuth && custAuth.customer) {
        HttpServer.json(res, 403, {
          success: false,
          error: 'Forbidden: Customers cannot access developer simulator endpoints.'
        });
        return null;
      }
    }

    return merchant;
  }

  // 1. GET /api/v1/admin/dev/simulator/customers (Search / Select Existing Customer)
  server.get('/api/v1/admin/dev/simulator/customers', async (req, res) => {
    const merchant = checkSimulatorAuth(req, res);
    if (!merchant) return;

    const q = (req.query.q || '').trim();
    let querySql = `
      SELECT id, name, email, mobile_number, status, created_at
      FROM customers
      WHERE status != 'BANNED'
    `;
    const params = [];
    if (q) {
      querySql += ` AND (name LIKE ? OR email LIKE ? OR mobile_number LIKE ?)`;
      const searchParam = `%${q}%`;
      params.push(searchParam, searchParam, searchParam);
    }
    querySql += ` ORDER BY created_at DESC LIMIT 30`;

    const rows = ctx.db.query(querySql, params);
    const customers = rows.map(r => ({
      id: r.id,
      name: r.name,
      email: r.email,
      phone: r.mobile_number,
      mobileNumber: r.mobile_number,
      status: r.status,
      createdAt: r.created_at
    }));

    HttpServer.json(res, 200, { customers });
  });

  // 2. GET /api/v1/admin/dev/simulator/customers/:id/sessions (Target Existing Payment Session)
  server.get('/api/v1/admin/dev/simulator/customers/:id/sessions', async (req, res, params) => {
    const merchant = checkSimulatorAuth(req, res);
    if (!merchant) return;

    const customer = ctx.customerRepo ? ctx.customerRepo.findById(params.id) : null;
    if (!customer) {
      HttpServer.json(res, 404, { error: 'Customer not found.' });
      return;
    }

    const rows = ctx.db.query(`
      SELECT id, order_id, amount_paise, status, device_id, method, created_at, expires_at
      FROM payment_sessions
      WHERE customer_id = ?
      ORDER BY created_at DESC
      LIMIT 20
    `, [customer.id]);

    const sessions = rows.map(r => ({
      id: r.id,
      paymentId: r.id,
      orderId: r.order_id,
      amount: toRupeesString(r.amount_paise),
      amountPaise: r.amount_paise.toString(),
      status: r.status,
      deviceId: r.device_id,
      method: r.method,
      isProcessable: ['ACTIVE', 'PROCESSING'].includes(r.status),
      createdAt: r.created_at,
      expiresAt: r.expires_at
    }));

    HttpServer.json(res, 200, {
      customer: {
        id: customer.id,
        name: customer.name,
        email: customer.email,
        phone: customer.mobileNumber,
        mobileNumber: customer.mobileNumber
      },
      sessions
    });
  });

  // 3. POST /api/v1/admin/dev/simulator/inject (Push SMS / Gmail Synthetic Observation into Real Pipeline)
  server.post('/api/v1/admin/dev/simulator/inject', async (req, res) => {
    const merchant = checkSimulatorAuth(req, res);
    if (!merchant) return;

    // Developer Test Mode Compatibility (Section 27 & Acceptance Test 36)
    const until = merchant?.branding?.testModeUntil;
    const isTestModeActive = Boolean(until && Number(until) > Date.now());
    if (!isTestModeActive) {
      HttpServer.json(res, 403, {
        success: false,
        error: 'Developer Test Mode is inactive or has expired. Please enable Test Mode to simulate observations.',
        testModeActive: false
      });
      return;
    }

    const body = await HttpServer.parseBody(req).catch(() => ({}));

    // Target Payment Session (Section 5 & 6)
    const paymentSessionId = body.paymentSessionId || body.sessionId || null;
    let targetSession = null;
    if (paymentSessionId) {
      targetSession = ctx.sessionRepo.findById(paymentSessionId);
      if (!targetSession) {
        HttpServer.json(res, 404, {
          success: false,
          error: `Payment session not found: ${paymentSessionId}`
        });
        return;
      }

      // Active Session Requirement (Section 6)
      if ([PaymentStatus.SUCCESS, PaymentStatus.FAILED, PaymentStatus.EXPIRED, PaymentStatus.CANCELLED].includes(targetSession.status)) {
        HttpServer.json(res, 400, {
          success: false,
          error: 'SESSION NO LONGER ACTIVE',
          reason: 'SESSION_NOT_ACTIVE',
          status: targetSession.status,
          message: 'SESSION NO LONGER ACTIVE: Observation injection is only permitted on ACTIVE or PROCESSING payment sessions.'
        });
        return;
      }
    }

    // Two Independent Streams: SMS and GMAIL (Section 8-15)
    // 100% customizable fields - ABSOLUTELY NO SIMULATOR PRE-VALIDATION!
    const rawSource = (body.source || body.stream || 'SMS').toString().trim().toUpperCase();
    const source = rawSource.includes('GMAIL') ? 'GMAIL' : 'SMS';
    const rawSender = (body.sender !== undefined ? body.sender : '').toString();
    const rawContent = (body.content !== undefined ? body.content : (body.message || body.raw || '')).toString();
    const rawReceivedAt = body.receivedAt !== undefined ? body.receivedAt : null;

    // Canonical Synthetic Observation Model (Section 16)
    const simUuid = crypto.randomUUID();
    const messageId = body.messageId || (source === 'GMAIL' ? `SIM-GMAIL-${simUuid}` : `SIM-SMS-${simUuid}`);

    const syntheticObservation = {
      source: source.toLowerCase(),
      message_id: messageId,
      sender: rawSender,
      received_at: rawReceivedAt ? String(rawReceivedAt) : new Date().toISOString(),
      body: rawContent,
      raw: rawContent, // Exact raw preserved
      is_simulated: true
    };

    // Invoke the REAL canonical verification pipeline (Section 17)
    const pipeline = ctx.pipeline || new PaymentVerificationPipeline({
      db: ctx.db,
      sessionRepo: ctx.sessionRepo,
      obsRepo: ctx.obsRepo,
      customerRepo: ctx.customerRepo,
      attributionEngine: ctx.attributionEngine,
      auditRepo: ctx.auditRepo,
      merchantRepo: ctx.merchantRepo,
      server,
      eventEmitter: ctx.eventEmitter
    });

    const pipelineResult = pipeline.process({
      source,
      rawText: rawContent,
      sender: rawSender,
      receivedAt: rawReceivedAt || undefined,
      paymentSessionId: targetSession?.id || undefined,
      receivingAccountId: targetSession?.receivingAccountId || undefined,
      customerId: body.customerId || targetSession?.customerId || undefined,
      isTest: true,
      testModeActive: true,
      suppressBan: Boolean(body.suppressBan),
      isSimulated: true,
      messageId,
      merchantId: merchant.id
    });

    // Audit Logging (Section 26 & Acceptance Test 33)
    if (ctx.auditRepo) {
      ctx.auditRepo.log({
        merchantId: merchant.id,
        entityType: 'PAYMENT_SIMULATION',
        entityId: messageId,
        action: 'SIMULATED_OBSERVATION_INJECTED',
        actorType: 'DEVELOPER',
        actorId: 'DEVELOPER_SIMULATOR',
        details: {
          developerIdentity: 'DEVELOPER',
          targetCustomer: body.customerId || targetSession?.customerId || null,
          targetSession: targetSession?.id || null,
          source,
          sender: rawSender,
          receivedAt: syntheticObservation.received_at,
          injectionTimestamp: Date.now(),
          simulationMessageId: messageId,
          exactRawMessage: rawContent,
          processingResult: pipelineResult.status,
          finalVerificationResult: pipelineResult.stages?.stage4?.status || 'UNKNOWN',
          securityResult: pipelineResult.stages?.stage5?.status || 'UNKNOWN',
          attributionResult: pipelineResult.stages?.stage6?.status || 'UNKNOWN',
          failingStage: pipelineResult.failingStage || null,
          failingReason: pipelineResult.reason || null
        }
      });
    }

    HttpServer.json(res, 200, {
      ...pipelineResult,
      isSimulated: true,
      syntheticObservation
    });
  });

  // 4. GET /api/v1/admin/dev/simulator/history (Recent Simulation Events)
  server.get('/api/v1/admin/dev/simulator/history', async (req, res) => {
    const merchant = checkSimulatorAuth(req, res);
    if (!merchant) return;

    const rows = ctx.db.query(`
      SELECT id, entity_type, entity_id, action, actor_type, details, created_at
      FROM audit_logs
      WHERE entity_type = 'PAYMENT_SIMULATION'
      ORDER BY created_at DESC
      LIMIT 30
    `);

    const history = rows.map(r => {
      let details = {};
      try { details = JSON.parse(r.details); } catch {}
      return {
        id: r.id,
        messageId: r.entity_id,
        action: r.action,
        timestamp: r.created_at,
        source: details.source || 'UNKNOWN',
        sender: details.sender || '',
        sessionId: details.targetSession || null,
        result: details.processingResult || 'UNKNOWN',
        failingReason: details.failingReason || null,
        details
      };
    });

    HttpServer.json(res, 200, { history });
  });

  // =========================================================================
  // SECURITY & BAN MANAGEMENT ROUTES (Sections 4, 5, 6, 7, 9)
  // =========================================================================

  // 1. GET /api/v1/admin/security/banned-users (List and Search Banned Users)
  server.get('/api/v1/admin/security/banned-users', async (req, res) => {
    const q = req.query.q || '';
    if (!ctx.customerRepo) {
      HttpServer.json(res, 200, { bannedUsers: [] });
      return;
    }
    const bannedUsers = ctx.customerRepo.findAllBanned(q);
    HttpServer.json(res, 200, { bannedUsers });
  });

  // 2. GET /api/v1/admin/security/banned-users/:id (View Complete Ban Details & Raw Evidence)
  server.get('/api/v1/admin/security/banned-users/:id', async (req, res, params) => {
    if (!ctx.customerRepo) {
      HttpServer.json(res, 404, { error: 'Customer repository not configured' });
      return;
    }
    const evidence = ctx.customerRepo.getBanEvidence(params.id);
    if (!evidence) {
      HttpServer.json(res, 404, { error: 'Customer not found' });
      return;
    }
    HttpServer.json(res, 200, evidence);
  });

  // 3. POST /api/v1/admin/security/unban (Unban Customer with Confirmation & Audit Log)
  server.post('/api/v1/admin/security/unban', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const customerId = body.customerId || body.id;
    if (!customerId) {
      HttpServer.json(res, 400, { error: 'Customer ID is required.' });
      return;
    }

    const customer = ctx.customerRepo.findById(customerId);
    if (!customer) {
      HttpServer.json(res, 404, { error: 'Customer not found.' });
      return;
    }

    const prevStatus = customer.status;
    const updated = ctx.customerRepo.unbanCustomer(customerId);

    // Record USER_UNBANNED in Audit Log (Section 5)
    if (ctx.auditRepo) {
      ctx.auditRepo.log({
        merchantId: req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001',
        entityType: 'CUSTOMER',
        entityId: customerId,
        action: 'USER_UNBANNED',
        actorType: 'ADMIN',
        actorId: req.headers['x-admin-id'] || 'ADMIN_USER',
        details: {
          adminIdentity: req.headers['x-admin-id'] || 'ADMIN_USER',
          customerIdentity: {
            id: customer.id,
            name: customer.name,
            email: customer.email,
            phone: customer.mobileNumber
          },
          timestamp: new Date().toISOString(),
          previousStatus: prevStatus,
          newStatus: 'ACTIVE'
        }
      });
    }

    // Also record security event for unban history
    if (ctx.customerRepo.createSecurityEvent) {
      ctx.customerRepo.createSecurityEvent({
        merchantId: req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001',
        customerId,
        eventType: 'USER_UNBANNED',
        fraudReason: 'RESTORED_BY_ADMIN',
        rawMessage: `Customer '${customer.name}' was unbanned by merchant/admin. Gateway access restored.`
      });
    }

    HttpServer.json(res, 200, {
      success: true,
      message: `Customer ${customer.name} unbanned. Gateway access restored.`,
      customer: updated
    });
  });

  // 4. POST /api/v1/admin/security/manual-ban (Manually Ban Customer with Distinct Reason)
  server.post('/api/v1/admin/security/manual-ban', async (req, res) => {
    const body = await HttpServer.parseBody(req).catch(() => ({}));
    const customerId = body.customerId || body.id;
    const reason = body.reason || 'MANUAL_BAN';
    const notes = body.notes || 'Manually banned by merchant operator';

    if (!customerId) {
      HttpServer.json(res, 400, { error: 'Customer ID is required.' });
      return;
    }

    const customer = ctx.customerRepo.findById(customerId);
    if (!customer) {
      HttpServer.json(res, 404, { error: 'Customer not found.' });
      return;
    }

    const prevStatus = customer.status;
    const updated = ctx.customerRepo.banCustomer(customerId, 'MANUAL_BAN', {
      lastDevice: body.deviceInfo || 'Manual Admin Action'
    });

    if (ctx.auditRepo) {
      ctx.auditRepo.log({
        merchantId: req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001',
        entityType: 'CUSTOMER',
        entityId: customerId,
        action: 'CUSTOMER_MANUALLY_BANNED',
        actorType: 'ADMIN',
        actorId: req.headers['x-admin-id'] || 'ADMIN_USER',
        details: {
          adminIdentity: req.headers['x-admin-id'] || 'ADMIN_USER',
          customerId,
          customerName: customer.name,
          reason: 'MANUAL_BAN',
          notes,
          timestamp: new Date().toISOString(),
          previousStatus: prevStatus,
          newStatus: 'BANNED'
        }
      });
    }

    if (ctx.customerRepo.createSecurityEvent) {
      ctx.customerRepo.createSecurityEvent({
        merchantId: req.headers['x-merchant-id'] || '00000000-0000-0000-0000-000000000001',
        customerId,
        eventType: 'MANUAL_BAN',
        fraudReason: 'MANUAL_BAN',
        rawMessage: `Manual ban applied by admin: ${notes}`
      });
    }

    HttpServer.json(res, 200, {
      success: true,
      message: `Customer ${customer.name} manually banned.`,
      customer: updated
    });
  });

  // 5. GET /api/v1/admin/security/events (List All Forensic Security Events)
  server.get('/api/v1/admin/security/events', async (req, res) => {
    const events = ctx.customerRepo ? ctx.customerRepo.getSecurityEvents(null, 100) : [];
    HttpServer.json(res, 200, { events });
  });

  // 6. GET /api/v1/admin/security/history (Ban/Unban History Log)
  server.get('/api/v1/admin/security/history', async (req, res) => {
    const logs = ctx.db.query(`
      SELECT id, entity_type, entity_id, action, actor_type, actor_id, details, created_at
      FROM audit_logs
      WHERE action IN ('CUSTOMER_AUTOMATICALLY_BANNED', 'CUSTOMER_MANUALLY_BANNED', 'USER_UNBANNED', 'TEST_MODE_FRAUD_FLAGGED_BAN_SUPPRESSED')
         OR entity_type = 'CUSTOMER'
      ORDER BY created_at DESC
      LIMIT 100
    `);
    const parsedLogs = logs.map(l => {
      let details = {};
      try { details = JSON.parse(l.details); } catch {}
      return {
        id: l.id,
        entityType: l.entity_type,
        entityId: l.entity_id,
        action: l.action,
        actorType: l.actor_type,
        actorId: l.actor_id,
        details,
        createdAt: l.created_at
      };
    });
    HttpServer.json(res, 200, { history: parsedLogs });
  });

  // 7. GET /api/v1/admin/customers (List active customers for manual ban selector)
  server.get('/api/v1/admin/customers', async (req, res) => {
    const rows = ctx.db.query('SELECT id, name, email, mobile_number, status, created_at FROM customers ORDER BY created_at DESC LIMIT 100');
    HttpServer.json(res, 200, {
      customers: rows.map(r => ({
        id: r.id,
        name: r.name,
        email: r.email,
        phone: r.mobile_number,
        status: r.status,
        createdAt: r.created_at
      }))
    });
  });
}