import { dbConnection } from "db";
import type { NextApiRequest, NextApiResponse } from "next";
import * as fs from 'fs';

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  try {
    if (req.method === "GET") {
      const db = await dbConnection;

      // Read SQL queries from file
      const sql1 = fs.readFileSync('sql/tables-sqlite.sql', 'utf-8');

      // Execute the SQL queries
      await db.exec(sql1);

      const sql2 = fs.readFileSync('sql/data.sql', 'utf-8');
      await db.exec(sql2);

      res.status(200).json("Database seeded");
    }
  } catch (error) {
    res.status(500).json(error.message);
  }
}
