import { FastifyError, FastifyReply, FastifyRequest } from 'fastify';
import { logger } from '../../../utils/logger';

export function mediaErrorHandler(
  error: FastifyError | Error,
  request: FastifyRequest,
  reply: FastifyReply
) {
  logger.error('Media API error', {
    error: error.message,
    stack: error.stack,
    url: request.url,
    method: request.method,
  });

  const statusCode = 'statusCode' in error ? error.statusCode ?? 500 : 500;
  reply.status(statusCode).send({
    message: statusCode === 500 ? 'Internal server error' : error.message,
  });
}
