import type { NextApiRequest, NextApiResponse } from 'next';

type ResponseData = {
  health?: string;
  success: boolean;
};

export default async function POST(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
  if (!req.body.url) {
    return res.status(404).json({
      success: false,
    });
  }

  try {
    const response = await fetch(`${req.body.url}?action=health-check`);

    const json = await response.json();

    if (!response.ok) {
      return res.status(404).json({
        success: false,
      });
    }
  } catch (e) {
    return res.status(404).json({
      success: false,
    });
  }

  res.json({
    success: true,
    health: 'ok',
  });
}
