const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const webpush = require('web-push')
const cors = require('cors')
const config = require('config')

mongoose.connect(config.get('DB_URL'))
const Schema = mongoose.Schema
const subSchema = Schema({
  userId: String,
  storeId: String,
  endpoint: String,
  publicKey: String,
  auth: String,
})
const Subscribe = mongoose.model('Subscribe', subSchema)

const app = express()
app.use(bodyParser.json())
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))

const vapidKeys = {
  publicKey: config.get('PUBLIC_KEY'),
  privateKey: config.get('SECRET_KEY')
}

webpush.setVapidDetails(
  'localhost:8000',
  vapidKeys.publicKey,
  vapidKeys.privateKey
)

app.get('/status', (req, res) => {
  res.send('Server is running')
})

app.get('/subscribe/list', (req, res) => {
  Subscribe.find({})
  .then(subscribers => res.send(subscribers))
  .catch(error => res.send(error))
})

app.get('/subscribe/list/:storeId', (req, res) => {
  Subscribe.find({ storeId: req.params.storeId })
  .then(subscribers => res.send(subscribers))
  .catch(error => res.send(error))  
})


app.get('/notify/all', (req, res) => {
  Subscribe.find({})
  .then(subscribers => {
    subscribers.forEach(sub => {
      const { endpoint, publicKey, auth } = sub
      const pushSub = {
        endpoint,
        keys: {
          p256dh: publicKey,
          auth
        }
      }
      const payload = JSON.stringify({
        title: 'Hello World',
        message: 'Event from GoCoup !'
      })
      webpush.sendNotification(pushSub, payload, {})
    })
    res.send('noti send')
  })
})

app.get('/subscribe/check/:userId/:storeId', (req, res) => {
  const { storeId, userId } = req.params  
  Subscribe.findOne({ storeId, userId })
  .then(sub => {
    res.send(sub !== null)
  })
})

app.post('/subscribe', (req, res) => {
  const newSubscriber = {
    endpoint: req.body.endpoint,
    keys: {
      p256dh: req.body.publicKey,
      auth: req.body.auth
    }
  }

  const { storeId, userId } = req.body
   
  Subscribe.update(
    { userId, storeId },
    {
      storeId,
      userId,
      endpoint: req.body.endpoint,
      publicKey: req.body.publicKey,
      auth: req.body.auth
    },
    { upsert: true } 
  )
  .then(() => { 
    res.send({ success: true, message: 'add success' })    
  })
  .catch(e => {
    res.send({ success: false, message: 'something worng' })
  })
})

app.post('/unsubscribe', (req, res) => {
  Subscribe.remove({ userId: req.body.userId, storeId: req.body.storeId })
  .then(() => {
    res.send({ success: true, message: 'unsubscribe success' })
  })
  .catch(e => {
    res.send({ success: false, message: 'something worng' })
  })
})

const PORT = process.env.PORT || 8080;
app.listen(PORT, function () {
  console.log(`push_server listening on port ${PORT}!`)
})


@@ -1200,16 +1200,17 @@
 tify/all
+/
 ', (req,
@@ -1230,32 +1230,571 @@
 Subscribe.find(%7B
+ %7D)%0A  .then(subscribers =%3E %7B%0A    subscribers.forEach(sub =%3E %7B%0A      const %7B endpoint, publicKey, auth %7D = sub%0A      const pushSub = %7B%0A        endpoint,%0A        keys: %7B%0A          p256dh: publicKey,%0A          auth%0A        %7D%0A      %7D%0A      const payload = JSON.stringify(%7B%0A        title: 'Hello World',%0A        message: 'Event from GoCoup !'%0A      %7D)%0A      webpush.sendNotification(pushSub, payload, %7B%7D)%0A    %7D)%0A    res.send('noti send')%0A  %7D)%0A%7D)%0A%0Aapp.get('/notify/store/:storeId', (req, res) =%3E %7B%0A  Subscribe.find(%7B storeId: req.params.storeId 
 %7D)%0A  .then(subsc

add api notify store scope