// 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 { inspect } from 'util';

import { cors } from '../../../lib/cors';

export default async function handler(req, res) 
{
	await cors(req, res);
	let { acc_number } = req.body;

	console.log("API", "auth/admin");
	if(req.headers.cookie !== undefined)
	{
		const cookies = cookie.parse(req.headers?.cookie ? req.headers?.cookie : "");

		if(cookies.jwt !== undefined && cookies.jwt !== null)
		{
			let allow = false;
			let company = {};
			let client_jwt_decoded = jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT);


			await axios.post(`${ process.env.NEXT_PUBLIC_API_HOST }/api/admin/users/`, {
				token: jwt.sign({ "acc_number": client_jwt_decoded.acc_number, }, process.env.JWT_SECRET_CRM, { noTimestamp: true }), 
			})
			.then((api_response) => 
			{
				console.log("API", "auth/admin", "RESPONSE");
				//console.log(inspect(api_response.data, true, null, true));
	
				res.status(200).send(api_response.data);
			})
			.catch((error) => 
			{
				console.error("API", "auth/admin", "error");
				console.error(error);
	
				res.status(403).json();
			});
		}
		else
		{
			res.status(403).json();
		}
	}
	else
	{
		res.status(403).json();
	}
}