// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import axios from 'axios';
import { Cookies } from 'react-cookie';
import cookie from 'cookie';
import moment from 'moment';
import jwt from 'jsonwebtoken';
import { cors } from '../../../lib/cors';

export default async function handler(req, res) 
{
	await cors(req, res);

	if(req.headers.cookie !== undefined)
	{
		const cookies = cookie.parse(req.headers?.cookie ? req.headers?.cookie : "");
		if(cookies.jwt !== undefined && cookies.jwt !== null)
		{
			if(jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT))
			{
				const response = await new Promise((resolve, reject) => 
				{
					axios.post(`${ process.env.NEXT_PUBLIC_API_HOST }/api/account/materials/`, {
					})
					.then((api_response) => 
					{
						console.log("RESPONSE");
						console.log(api_response.data);

						resolve(api_response.data);
					})
					.catch((error) => 
					{
						console.log("error");
						console.error(error);

						reject([]);
					});
				});

				res.status(200).json(response);
			}
			else
			{
				res.status(403);
			}
		}
		else
		{
			res.status(403);
		}
	}
}