import { db } from "@/helpers/api/db";


export default async function handler(req, res) {
	if (!db.initialized) {
		await db.initialize();
	}

     const { parentID } = req.query;
     
     if (!/^\d+$/.test(parentID)) {
          return res.status(400).json({ error: "Invalid parentID. Must be an integer." });
      }
      
	try {
          const subcategories = await db.Categories.findAll({
               where: {
                    parentID: parentID
               },
               raw:true 
          });

		return res.status(200).json({ subcategories });
	} catch (error) {
		console.error(error);
		return res
			.status(500)
			.json({ error: "Internal server error", status: 500 });
	}
}
