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

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

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

// router.post('/create', authorize(), upload.single('app_logo'), create);
router.post('/create', authorize(), upload.fields([
    { name: 'app_logo', maxCount: 1 },
    { name: 'app_logo_secondary', maxCount: 1 },
    { name: 'app_logo_navbar', maxCount: 1 }
]), create);
router.get('/', authorize(), getAll);
router.get('/get-logo', getLogo);
router.get('/filter', authorize(), filter);
router.get('/option', authorize(), option);
router.get('/count', authorize(), count);
// router.put('/:id', authorize(), upload.single('app_logo'), update);
router.put('/:id', authorize(), upload.fields([
    { name: 'app_logo', maxCount: 1 },
    { name: 'app_logo_secondary', maxCount: 1 },
    { name: 'app_logo_navbar', maxCount: 1 }
]), update);
router.put('/enabled/:id', authorize(), enabled);
router.delete('/:id', authorize(), _delete);

module.exports = router;

function create(req, res, next) {
    const userData = {
        ...req.body,
        app_logo: req.files['app_logo'] ? req.files['app_logo'][0].filename : null,
        app_logo_secondary: req.files['app_logo_secondary'] ? req.files['app_logo_secondary'][0].filename : null,
        app_logo_navbar: req.files['app_logo_navbar'] ? req.files['app_logo_navbar'][0].filename : null
    };

    appService.create(userData)
        .then(result => res.json(result))
        .catch(next);
}

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

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

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

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

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

function update(req, res, next) {
    const updateData = {
        ...req.body,
        app_logo: req.files['app_logo'] ? req.files['app_logo'][0].filename : req.body.app_logo ? req.body.app_logo : null,
        app_logo_secondary: req.files['app_logo_secondary'] ? req.files['app_logo_secondary'][0].filename : req.body.app_logo_secondary ? req.body.app_logo_secondary : null,
        app_logo_navbar: req.files['app_logo_navbar'] ? req.files['app_logo_navbar'][0].filename : req.body.app_logo_navbar ? req.body.app_logo_navbar : null
    };

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

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

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

