var express = require('express');
var consultasDB = require ('../database/consultas');
var router = express.Router();

router.get('/users', async function(req, res) {

    var users = await consultasDB.getUsers();
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify(users));
})


router.post('/users', async function(req, res) {
    const userIn  = req.body;
    if(!userIn){
        return res.status(500).send("Datos incorrectos.")
    }
    console.log(userIn);
    userOut = await consultasDB.userLogin(userIn.loginUser, userIn.loginPassword);
    
    if (userOut){
        console.log(JSON.stringify(userOut));
        res.send(JSON.stringify(userOut));}
})

router.post('/discoteca', async function(req, res){
    console.log(req.body);
    const discotecaId = req.body;
    if(!discotecaId){
        return res.status(500).send("Datos incorrectos.")
    }
    discoteca = await consultasDB.getDiscoteca(discotecaId.id);
    if(discoteca){
        console.log(JSON.stringify(discoteca));
        res.send(JSON.stringify(discoteca));
    }
})

router.post('/evento', async function(req, res){
    console.log(req.body);
    const evento = req.body;
    eventout = await consultasDB.addEvento(evento);
    if (eventout){
        console.log(JSON.stringify(eventout));
        res.send(JSON.stringify(eventout));
    }
})

router.get('/evento', async function(req, res){
    var eventosout = await consultasDB.getEventos();
    res.setHeader('Content-Type', 'application/json');
    if (eventosout){
        res.send(JSON.stringify(eventosout));
    }
})



router.post('/eventosDiscoteca', async function(req, res){
    console.log(req.body);
    const evento = req.body;
    eventout = await consultasDB.getEventosDiscoteca(evento);
    if (eventout){
        console.log(JSON.stringify(eventout));
        res.send(JSON.stringify(eventout));
    }
})

module.exports = router; 