import type { Request, Response } from 'express';
import { errorResponse } from '../utils/response.js';

// Registered last for a known path (after its real GET/POST/etc handlers), so
// a request only reaches this if the path matched but the method didn't —
// giving a 405 instead of a generic 404 for wrong-method requests.
export default function methodNotAllowed(...allowedMethods: string[]) {
  return (req: Request, res: Response) => {
    res.set('Allow', allowedMethods.join(', '));
    return void errorResponse(res, `Method ${req.method} not allowed on ${req.originalUrl}`, 405);
  };
}
