module.exports = Self => {
    Self.remoteMethodCtx('upsertItem', {
        description: 'Add a record or update it if it already exists.',
        accessType: 'WRITE',
        accepts: [{
            arg: 'shelvingFk',
            type: 'string',
            required: true,
        },
        {
            arg: 'items',
            type: ['number'],
            required: true,
            description: 'array of item foreign keys'
        },
        {
            arg: 'warehouseFk',
            type: 'number',
            required: true
        }],

        http: {
            path: `/upsertItem`,
            verb: 'POST'
        }
    });

    Self.upsertItem = async(ctx, shelvingFk, items, warehouseFk, options) => {
        const myOptions = {userId: ctx.req.accessToken.userId};
        let tx;

        if (typeof options == 'object')
            Object.assign(myOptions, options);

        if (!myOptions.transaction) {
            tx = await Self.beginTransaction({});
            myOptions.transaction = tx;
        }

        const discardItems = new Set();
        const itemCounts = items.reduce((acc, item) => {
            acc[item] = (acc[item] || 0) + 1;
            return acc;
        }, {});

        try {
            for (let item of items) {
                if (!discardItems.has(item)) {
                    let quantity = itemCounts[item];
                    discardItems.add(item);

                    await Self.rawSql('CALL vn.itemShelving_add(?, ?, ?, NULL, NULL, NULL, ?)',
                        [shelvingFk, item, quantity, warehouseFk], myOptions
                    );
                }
            }

            if (tx) await tx.commit();
        } catch (e) {
            if (tx) await tx.rollback();
            throw e;
        }
    };
};