// import Fastify, { FastifyRequest, FastifyReply } from 'fastify';
// import Rollbar from 'rollbar';

// const app = Fastify();

// // === Map of project IDs to Rollbar tokens ===
// const tokenMap = new Map<string, string>([
//   ['ac-projects', '741d790f170c4e22b8583383b8b92e52'],
//   ['dad-projects', '99034fb63b4f468f8e9afe087cca34f0'],
// ]);

// // === Cache of Rollbar instances ===
// const rollbarCache = new Map<string, Rollbar>();

// function getRollbar(project: string): Rollbar | null {
//   const token = tokenMap.get(project);
//   if (!token) return null;

//   if (!rollbarCache.has(project)) {
//     const instance = new Rollbar({
//       accessToken: token,
//       captureUncaught: false,
//       captureUnhandledRejections: false,
//     });
//     rollbarCache.set(project, instance);
//   }
//   return rollbarCache.get(project)!;
// }

// // === Test route that throws error ===
// app.get('/', async (req: FastifyRequest, reply: FastifyReply) => {
//   throw new Error('This is a test error');
// });

// // === Per-project manual log route ===
// app.post<{
//   Params: { project: string };
//   Body: { message: string };
// }>('/:project/logs', async (req, reply) => {
//   const { project } = req.params;
//   const { message } = req.body;

//   const rollbar = getRollbar(project);
//   if (!rollbar) {
//     return reply.status(400).send({ error: 'Invalid project' });
//   }

//   rollbar.log(message);
//   reply.send({ status: 'Logged to Rollbar', project });
// });

// // === Global error handler ===
// app.setErrorHandler((error, req, reply) => {
//   const url = req.routerPath ?? req.url;
//   const project = req.params?.['project'];

//   // Try get Rollbar for dynamic project path
//   const rollbar = typeof project === 'string' ? getRollbar(project) : null;

//   if (rollbar) {
//     rollbar.error(error, req);
//   } else {
//     // Default logger fallback (you could set a default Rollbar instance)
//     console.error('Unhandled Error:', error);
//   }

//   reply.status(500).send({ error: 'Internal Server Error' });
// });

// // === Start server ===
// app.listen({ port: 3000 }, (err) => {
//   if (err) {
//     console.error('Failed to start:', err);
//     process.exit(1);
//   }
//   console.log('🚀 Server running at http://localhost:3000');
// });