import { apiHandler, db } from '@/helpers/api';

export default apiHandler({
     get: getUserData
});

async function getUserData(req, res) {
     const userID_admin = req.userID;
     const userID = req.query.userID; 

     if(!db.initialized)
          await db.initialize(); 

     try {
          const userData_admin = await db.User.findByPk(userID_admin);
          
          if (!userData_admin) 
               return res.status(401).json({ error: 'Unknown!' });

          if(userData_admin.admin === 0) 
               return res.status(401).json({ error: "Unauthorized!" }); 

          const userData = await db.User.findByPk(userID); 

          const responseArray = [];

          responseArray.push({ userData});

          return res.status(200).json(responseArray);
     } catch (error) {
          return res.status(500).json({ error: 'Eroare la preluarea datelor utilizatorului.' });
     }
}