// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import fs from 'fs';
import path from 'path';
import csv from 'csv-parser';

export default function handler(
  req: NextApiRequest,
  res: NextApiResponse<any>,
) {
  const csvFilePath = path.join(process.cwd(), 'public', 'Stars_Names.csv');
  const results: any[] = [];
  fs.createReadStream(csvFilePath)
    .pipe(csv())
    .on('data', (data) => results.push(data))
    .on('end', () => {
      const names: any = []
      results.map((item)=>{
        names.push(item["Name"])
      })
      names.sort(() => Math.random() - 0.5);
      res.status(200).json({ names })
    })
    .on('error', (error) => {
      res.status(500).json({ error: error.message });
    });
  // res.status(200).json({ name: "John Doe" });
}
