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

type ResponseData = {
  message: string;
};

export default async function POST(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
  const response = await fetch(`${req.headers['x-echo-url']}?action=sync&source=devStudio`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-novu-anonymous': req.headers['x-novu-anonymous'] as string,
    },
    body: JSON.stringify({ echoUrl: req.body.echoUrl }),
  });
  const json = await response.json();

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

  res.json(json);
}
