const express = require("express")
    , amazon = require('./src/Amazon');


// cron.schedule('0 * * * *', () => {
//     melon.methods.ccm();
//     console.log('Fetching Melon CCM List Successfully');
// });

var app = express();
app.use(express.json())
app.listen(20111, () => {
    console.log("Server running on port 20111");
});


app.get('/product/detail/:asin', (req, res, next) => {
    amazon.us.task.productDetail(req.params.asin).then((data) => {        
        res.json({ code: 200, data: data })
    })
})

app.post('/product/detail/batch', async (req, res, next) => {
    let result = []
    console.log(req.body.asins)
    for (promise of req.body.asins.map(async (asin, k) => {
        return await amazon.us.task.productDetail(asin)
    })) {
        await promise.then((data) => {            
            result.push(data);
        })
    }
    res.json({ code: 200, data: result })
})

app.get('/product/offers/:asin', (req, res, next) => {
    amazon.us.task.offerListing(req.params.asin).then((data) => res.json({ code: 200, data: data }))
})

app.get('/categories', (req, res, next) => {
    amazon.us.task.categories().then((data) => res.json({ code: 200, data: data }))
})

app.patch('/subcategories', (req, res, next) => {
    amazon.us.task.subCategories({ link: req.body.category_link }).then((data) => res.json({ code: 200, data: data }))
})