const express = require('express');
const router = express.Router();
const multer = require('multer');
const authorize = require('../../../../middlewares/authorize-sapps');
const questionService = require('./question.service');

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'D:/Development/BELAJAR/APPS/SAPPS-BACKEND/public/data/images/');
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + '-' + file.originalname);
    }
});

const upload = multer({ storage: storage });

router.post('/create', authorize(), upload.single('filename'), create);
router.post('/save-user-answer', authorize(), saveUserAnswer);
router.get('/', authorize(), getAll);
router.get('/filter', authorize(), filter);
router.get('/filter-user-answer', authorize(), filterUserAnswer);
router.get('/get-user-answer-by-user-id', authorize(), getUserAnswerByUserId);
router.get('/option', authorize(), option);
router.get('/count', authorize(), count);
router.get('/is-user-answer-exist', authorize(), isUserAnswerExist);
router.get('/get-user-answer', authorize(), getUserAnswer);
router.put('/:id', authorize(), upload.single('filename'), update);
router.put('/enabled/:id', authorize(), enabled);
router.delete('/:id', authorize(), _delete);

module.exports = router;

function create(req, res, next) {
    const questionData = {
        ...req.body,
        filename: req.file ? req.file.filename : null
    };

    questionService.create(questionData)
        .then(result => res.json(result))
        .catch(next);
}

function getAll(req, res, next) {
    questionService.getAll()
        .then(result => res.json(result))
        .catch(next);
}

function filter(req, res, next) {
    questionService.filter(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function option(req, res, next) {
    questionService.option()
        .then(result => res.json(result))
        .catch(next);
}

function count(req, res, next) {
    questionService.count()
        .then(result => res.json(result))
        .catch(next);
}

function update(req, res, next) {
    const updateData = {
        ...req.body,
        filename: req.file ? req.file.filename : req.body.filename ? req.body.filename : null
    };

    questionService.update(req.params.id, updateData)
        .then(result => res.json(result))
        .catch(next);
}

function enabled(req, res, next) {
    questionService.enabled(req.params.id, req.body)
        .then(result => res.json(result))
        .catch(next);
}

function _delete(req, res, next) {
    questionService.delete(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}

function saveUserAnswer(req, res, next) {
    questionService.saveUserAnswer(req.body)
        .then(result => res.json(result))
        .catch(next);
}

function isUserAnswerExist(req, res, next) {
    questionService.isUserAnswerExist(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function getUserAnswer(req, res, next) {
    questionService.getUserAnswer(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function filterUserAnswer(req, res, next) {
    questionService.filterUserAnswer(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function getUserAnswerByUserId(req, res, next) {
    questionService.getUserAnswerByUserId(req.query)
        .then(result => res.json(result))
        .catch(next);
}
