import { Express } from 'express';
import { RouteGenerateResC } from './route-generate-res';

export const routeGenerate = (app: Express) => {
    app.post('/generate', async (req, res) => {
        try {
            console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> start generate:', new Date());
            const data = await fetch(process.env.GENERATE_API_URL || '', {
                "headers": {
                    "accept": "application/json",
                    "accept-language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
                    "content-type": "application/json"
                },
                "referrer": "http://172.28.214.84:8001/docs",
                "body": JSON.stringify( req.body),
                "method": "POST",
                "mode": "cors",
                "credentials": "include",
            }).catch(e => {
                console.log('>>> E 2', e)
            });
            console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> end generate:', new Date());

            const reJsonedData: unknown = await data?.json();
            const checkedData = RouteGenerateResC.parse(reJsonedData);
            return res.json(checkedData)
        } catch (e) {
            console.log(e);
            res.status(500).end();
        }

    });
}