import { Response } from 'express';

/**
 * Send a successful JSON response.
 * Uses the standard format: { error: null, data }
 */
export function sendSuccess<T>(res: Response, data: T, status: number = 200) {
  res.status(status).json({ error: null, ...(data as any) });
}

/**
 * Send an error JSON response.
 * Uses the standard format: { error: string }
 * All routes should use this for consistent frontend error handling.
 */
export function sendError(res: Response, status: number, message: string) {
  res.status(status).json({ error: message });
}

/**
 * Send a 500 error response from a caught exception.
 * Prevents leaking stack traces in production.
 */
export function sendServerError(res: Response, err: unknown, fallbackMessage: string = 'Internal server error') {
  const message = err instanceof Error ? err.message : fallbackMessage;
  res.status(500).json({ error: message || fallbackMessage });
}
