const axios = require('axios');
const UserError = require('vn-loopback/util/user-error');
const moment = require('moment');

module.exports = Self => {
    Self.remoteMethod('sendOrders', {
        description: 'Sends a set of orders',
        accessType: 'WRITE',
        accepts: [{
            arg: 'tickets',
            type: ['number'],
            required: true
        }
        ],
        returns: {
            type: 'string',
            root: true
        },
        http: {
            path: `/sendOrders`,
            verb: 'POST'
        }
    });
    Self.sendOrders = async tickets => {
        const config = await Self.app.models.QuadmindsApiConfig.findOne();
        if (!config) throw new UserError('Config params not set');

        if (tickets.length > config.maxObjects)
            throw new UserError(`Quadminds does not support more than ${config.maxObjects} tickets`);

        let poisData = [];
        let isOk;
        for (let offset = 0; !isOk; offset = offset + config.limit) {
            const pois = await axios.get(`${config.url}pois/search?limit=${config.limit}&offset=${offset}`, {
                headers: {
                    'Accept': 'application/json',
                    'X-Saas-Apikey': config.key
                }
            });
            pois.data.data.length ? poisData.push(...pois.data.data) : isOk = true;
        }

        const poiMap = new Map(poisData.map(poi => [poi.code, poi._id]));

        let orders = await Self.rawSql(`
            SELECT a.id poiCode,
                    t.id code,
                    t.shipped date,
                    'PEDIDO' operation,
                    t.totalWithVat totalAmount,
                    t.totalWithoutVat totalAmountWithoutTaxes,
                    SUM(sv.volume) volume
                FROM ticket t
                    JOIN address a ON a.id = t.addressFk
                    JOIN saleVolume sv ON sv.ticketFk = t.id
                WHERE t.id IN (?)
                GROUP BY t.id
        `, [tickets]);

        // Transformo code en string ya que lo obtenermos como integer
        orders = orders.map(order => {
            return {
                ...order,
                poiId: poiMap.get(order.poiCode.toString()) || undefined,
                code: order.code.toString(),
                date: moment(order.date).format('YYYY-MM-DD'),
                totalAmount: order.totalAmount || undefined,
                totalAmountWithoutTaxes: order.totalAmountWithoutTaxes || undefined,
                timeWindow: [{
                    from: config.orderTimeFrom,
                    to: config.orderTimeTo
                }],
                orderMeasures: [{
                    constraintId: 3, // Volumen
                    value: order.volume
                }]
            };
        });

        await axios.post(`${config.url}orders`, orders, {
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'X-Saas-Apikey': config.key
            }
        });
    };
};