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

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

  try {
    const response = await fetch(`${req.headers['x-echo-url']}?action=discover`, {
      headers: {
        'Content-Type': 'application/json',
      },
    });

    const json = await response.json();

    res.json(json.workflows);
  } catch (e: any) {
    res.status(e.status || 500).send(e.message || 'Error occurred');
  }
}
