import type { NextApiRequest, NextApiResponse } from 'next';
import { getEchoHeaders } from '@/src/components/utils';

export default async function POST(req: NextApiRequest, res: NextApiResponse<any>) {
  if (!req.headers['x-echo-url']) {
    return res.status(404).send('No URL provided');
  }

  const response = await fetch(`${req.headers['x-echo-url']}?action=diff`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-novu-anonymous': req.headers['x-novu-anonymous'] as string,
    },
    body: JSON.stringify({
      echoUrl: req.headers['x-echo-url'],
    }),
  });
  const json = await response.json();

  if (json.error) {
    return res.status(json.statusCode || 500).json(json);
  }

  res.json(json);
}
