import { Request, Response, NextFunction } from 'express';
import { ZodSchema } from 'zod';
import { sendError } from '../utils/apiResponse';

export const validate = (schema: ZodSchema<any>) => async (req: Request, res: Response, next: NextFunction) => {
    try {
        await schema.parseAsync({
            body: req.body,
            query: req.query,
            params: req.params,
        });
        return next();
    } catch (error: any) {
        console.error('Validation error:', {
            body: req.body,
            error: error.errors || error
        });
        return sendError(res, `Validation Error: ${JSON.stringify(error.errors || error)}`, 400);
    }
};
