"use strict";

import express from "express";
const router = express.Router();

/*
 * Temporary debug endpoint to help trace requests reaching the Express
 * application. This will return the raw URL, method, and headers as seen
 * by the server. It is mounted under the application's basePath so you can
 * hit: /<basePath>/__debug/request
 */
router.get('/__debug/request', (req: express.Request, res: express.Response) => {
  try {
    console.log('[debug] __debug/request hit:', req.method, req.originalUrl);
    // Echo back a compact JSON summary so curl or browsers can inspect it.
    res.json({
      seenUrl: req.originalUrl,
      url: req.url,
      method: req.method,
      headers: req.headers,
      hostname: req.hostname,
      basePath: req.app && req.app.get && req.app.get('basePath')
    });
  } catch (e: any) {
    console.error('[debug] error in __debug/request', e && e.stack || e);
    res.status(500).json({ error: 'debug endpoint error' });
  }
});

export default router;
