import {GoodsAttributes, Goods, sequelize} from "../../models/index";
const {Op} = sequelize;

module.exports = async (req, res, next) => {

    //   schema for "where"(filter)

    const goodsFilter = {
        where: {
            goodsGroupId: {
                [Op.or]: req.params.id
            }
        },
        include: [
            {
                model: GoodsAttributes,
                attributes: {
                    exclude: ["createdAt", "updatedAt", 'goodsId']
                }
            }
        ],
        attributes: ["id", "goodsName", "goodsDescription"]
    };

    if(req.query.blackListIdsGoods && req.query.blackListIdsGoods.length > 0) {
        Object.assign(goodsFilter.where, {id: {
            [Op.notIn]: req.query.blackListIdsGoods
        }})
    }

    if(req.query.typeGoods == "распродажа") {
        Object.assign(goodsFilter.where, {isSale: true});
    }
    if(req.query.typeGoods == "новинки") {
        Object.assign(goodsFilter, {limit: 15});
    }
    if(req.query.brand) {
        Object.assign(goodsFilter.where, {goodsBrandId: {
            [Op.or]: req.query.brand
        }})
    }
    if(req.query.gender) {
        Object.assign(goodsFilter.where, {goodsGender: {
            [Op.or]: req.query.gender
        }})
    }
    if(req.query.madeIn) {
        Object.assign(goodsFilter.where, {goodsMadeIn: {
            [Op.or]: req.query.madeIn
        }})
    }
    if(req.query.volume) {
        req.query.volume.map((elem, index) => {
            req.query.volume[index] = parseInt(elem, 10);
        });
        Object.assign(goodsFilter.where, {volumesArray: {
            [Op.overlap]: req.query.volume
        }});
        // console.log(req.query.volume);
        // const id = [];
        // req.query.volume.map((elem, index) => {
        //     req.query.volume[index] = parseInt(elem, 10) + ''
        // });
        // // minus 4 hours
        // const idsGoods =
        //     await GoodsAttributes.findAll( { where: {valueAttribute: {[Op.or]: req.query.volume}} } );
        //
        // idsGoods.map(elem => {
        //     id.push(elem.dataValues.goodsId);
        // });
        // Object.assign(goodsFilter.where, {id: {
        //     [Op.in]: id
        // }});

    }
    if(req.query.sort && req.query.order) {

        if(req.query.sort !== "price") {
            Object.assign(goodsFilter, {
                order: [
                    ['GoodsAttributes', 'valueAttribute', 'asc'],
                    [req.query.sort, req.query.order]
                ]
            });
        }else {
            Object.assign(goodsFilter, {
                order: [
                    ['GoodsAttributes', 'valueAttribute', 'asc'],
                    ['GoodsAttributes', 'price', req.query.order]
                ]
            });
        }
    }else if(req.query.sort === 'random'){
        Object.assign(goodsFilter, {
            order: [['GoodsAttributes', 'valueAttribute', 'asc'], sequelize.random()]
        })
    }

    req.body.goodsFilter = goodsFilter;
    next();
};