import express from 'express';
import dotenv from 'dotenv';
import authRoutes from './routes/auth.js';
import helpRequestRoutes from './routes/helpRequests.js';
import offerRoutes from './routes/offers.js';
import reviewRoutes from './routes/reviews.js';
import addressRoutes from './routes/addresses.js';
import contactRoutes from './routes/contacts.js';
import profileRoutes from './routes/profile.js';
// import { requestLogger } from './middleware/logger.js'; // Temporarily removed for compatibility
import { rateLimit, authRateLimit } from '../middleware/rateLimit.cjs';

dotenv.config();

const app = express();
app.use(express.json());

app.get('/health', (_req, res) => res.json({ status: 'ok' }));

// Test route for role middleware
app.get('/test-role', (_req, res) => {
  res.status(200).json({ message: 'Test route for role middleware' });
});

// Metrics endpoint
app.get('/metrics', (_req, res) => {
  const uptime = process.uptime();
  const memoryUsage = process.memoryUsage();
  
  res.json({
    uptime: uptime,
    memory: {
      rss: memoryUsage.rss,
      heapTotal: memoryUsage.heapTotal,
      heapUsed: memoryUsage.heapUsed,
      external: memoryUsage.external
    },
    timestamp: new Date().toISOString()
  });
});

// Rate limiting für Auth-Endpunkte
app.use('/auth', authRateLimit(), authRoutes);

// Rate limiting für write-heavy Endpunkte
app.use('/requests', rateLimit({ max: 50 }), helpRequestRoutes);
app.use('/offers', rateLimit({ max: 50 }), offerRoutes);
app.use('/reviews', rateLimit({ max: 50 }), reviewRoutes);
app.use('/addresses', rateLimit({ max: 50 }), addressRoutes);
app.use('/contacts', rateLimit({ max: 50 }), contactRoutes);
app.use('/profile', rateLimit({ max: 50 }), profileRoutes);

const port = Number(process.env.PORT || 3000);
app.listen(port, () => {
  console.log(`helpyourneighbour backend listening on ${port}`);
});
