// 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 : "");

		console.log("req.body");
		console.log(req.body);

		if(cookies.jwt !== undefined && cookies.jwt !== null)
		{
			console.log("cookies.jwt");
			console.log(cookies.jwt);

			var client_jwt_decoded = jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT);
			var crm_jwt = jwt.sign(client_jwt_decoded, process.env.JWT_SECRET_CRM, { noTimestamp: true });

			console.log("client_jwt_decoded", client_jwt_decoded);
			console.log("crm_jwt", crm_jwt);

			console.log(`${ process.env.CRM_API_HOST }/lk/Contract/GetOsago`);

			const result = {
				osago: null, 
				kasko: null, 
				nsib: null, 
			};

			await Promise.all([ 
				new Promise((resolve) => {
					axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetOsago`, {
						params: { ...client_jwt_decoded, contract_number: req.body.number }, 
						headers: { "Authorization": `Bearer ${ crm_jwt }`, }, 
					})
					.then((crm_response) => { result.osago = crm_response.data; resolve(); })
					.catch((error) => { console.error(error); resolve(); });
				}), 
				new Promise((resolve) => {
					axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetKasko`, {
						params: { ...client_jwt_decoded, contract_number: req.body.number }, 
						headers: { "Authorization": `Bearer ${ crm_jwt }`, }, 
					})
					.then((crm_response) => { result.kasko = crm_response.data; resolve(); })
					.catch((error) => { console.error(error); resolve(); });
				}), 
				new Promise((resolve) => {
					axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetNsib`, {
						params: { ...client_jwt_decoded, contract_number: req.body.number }, 
						headers: { "Authorization": `Bearer ${ crm_jwt }`, }, 
					})
					.then((crm_response) => { result.nsib = crm_response.data; resolve(); })
					.catch((error) => { console.error(error); resolve(); });
				}), 
				new Promise((resolve) => {
					axios.get(`${ process.env.CRM_API_HOST }/lk/Contract/GetFinGap`, {
						params: { ...client_jwt_decoded, contract_number: req.body.number }, 
						headers: { "Authorization": `Bearer ${ crm_jwt }`, }, 
					})
					.then((crm_response) => { 
						result.fingap = crm_response.data;
						/*result.fingap = [
							{
								paid: true,
								period_type: "current", 
								invoice_url: null,
								company: "АО \"ГСК \"ЮГОРИЯ\"", 
								site: null,
								phone: null,
								number: "ТестФингап",
								url: null,
								period: "14.07.2021 - 13.07.2022", 
								amount: 1200000.00
							}, 
							{
								paid: false, 
								period_type: "prolong", 
								invoice_url: "353082cc-f38f-4da6-bcd4-3a6048eceb10", 
								company: "АО \"ГСК \"ЮГОРИЯ\"", 
								site: null, 
								phone: null, 
								number: "ТестФингап", 
								url: null, 
								period: "14.07.2022 - 13.07.2023", 
								amount: null 
							}
						];*/
						resolve(); 
					})
					.catch((error) => { console.error(error); resolve(); });
				}), 
			])
			.then(() => 
			{
				res.status(200).json(result);
			});
		}
		else
		{
			res.status(403);
		}
	}
}