const shared_consts = require('../tools/shared_nodejs');

const express = require('express');
const router = express.Router();

const tools = require('../tools/general');

var server_constants = require('../tools/server_constants');

var { Project } = require('../models/project');

var { authenticate, auth_default } = require('../middleware/authenticate');

var mongoose = require('mongoose').set('debug', false)
const Subscription = require('../models/subscribers');

const _ = require('lodash');

const { ObjectId } = require('mongodb');

const { City } = require('../models/city');


//POST 
router.post('/geojson', authenticate, function (req, res, next) {
  let prov = req.body.prov;
  // let idapp = req.body.idapp;
  return City.getGeoJsonByProvince(prov)
    .then((ris) => {

      if (ris)
        return res.send({ code: server_constants.RIS_CODE_OK, ris });

    }).catch(err => {
      console.log('ERR:', err);
      return res.status(400).send();
    });

});

router.post('/updateord', authenticate, async (req, res) => {
  const idOrdersCart = req.body.idOrdersCart;
  const idOrder = req.body.idOrder;
  const paramstoupdate = req.body.paramstoupdate;

  try {
    // Aggiorno l'Ordine Singolo
    await Order.updateOrderByParams(idOrder, paramstoupdate);

    // Aggiorno il Totale degli Ordini (OrdersCart)
    await OrdersCart.updateOrdersCartTotals(idOrdersCart, true)
      .then((orderscart) => {
        return res.send({ code: server_constants.RIS_CODE_OK, orderscart });
      }).catch(err => {
        console.log('ERR:', err);
        return res.status(400).send();
      });
  } catch (e) {
    console.error('Err', e);
  }
});


router.post('/update', authenticate, async (req, res) => {
  let orderscart = null;
  const idOrdersCart = req.body.idOrdersCart;
  const paramstoupdate = req.body.paramstoupdate;

  try {
    // Aggiorno Orderscart con i parametri passati
    orderscart = await OrdersCart.updateOrdersCartByParams(idOrdersCart, paramstoupdate);

    let updatetotals = true;
    if (paramstoupdate && paramstoupdate.hasOwnProperty('totalPrice')) {
      updatetotals = false;
    }
    if (updatetotals) {

      // Aggiorno il Totale degli Ordini (OrdersCart)
      await OrdersCart.updateOrdersCartTotals(idOrdersCart, true)
        .then((orderscart) => {
          return res.send({ code: server_constants.RIS_CODE_OK, orderscart });
        }).catch(err => {
          console.log('ERR:', err);
          return res.status(400).send();
        });
    } else {
      return res.send({ code: server_constants.RIS_CODE_OK, orderscart });
    }
  } catch (e) {
    console.error('Err', e);
  }

});


router.post('/sendmail', authenticate, async function (req, res, next) {
  const idOrdersCart = req.body.idOrdersCart;
  const idapp = req.body.idapp;
  const test = req.body.test;
  const previewonly = req.body.previewonly;
  const templemail_id = req.body.templemail_id;

  try {
    // Invia Email
    let myOrderCart = await OrdersCart.getOrdersCartById(idOrdersCart);
    if (myOrderCart) {
      const userto = myOrderCart.user;
      let emailto = userto.email;

      if (userto && emailto) {
        const emailsend = await sendemail.sendEmail_byTemplate(idapp, userto, previewonly, emailto, templemail_id, test);

        return res.send({ code: server_constants.RIS_CODE_OK, emailsend });
      }
    }

    return res.send({ code: server_constants.RIS_CODE_OK, emailsend: false });

  } catch (e) {
    console.error('err:', e);
    return res.status(400).send(e);
  }

});

module.exports = router;
