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<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=preview&workflowId=${req.query.workflowId}&stepId=${req.query.stepId}`,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        inputs: req.body.inputs || {},
      }),
    }
  );

  const json = await response.json();

  res.json(json);
}
