import { Request, Response, NextFunction } from 'express';
import { AppError } from '../utils/errors';
import { ZodError } from 'zod';

export const errorHandler = (err: any, req: Request, res: Response, next: NextFunction) => {
  console.error('[Error]:', err.message);
  
  if (err instanceof AppError) {
    return res.status(err.statusCode).json({ error: err.message });
  }
  
  if (err instanceof ZodError) {
    return res.status(400).json({ error: 'Validation Error', details: err.issues });
  }
  
  return res.status(500).json({ error: 'Internal Server Error' });
};
