const express = require('express');
const router = express.Router();
const multer = require('multer');
const authorize = require('../../../../middlewares/authorize-sapps');
const projectService = require('./project.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('project_logo'), create);
router.post('/create', authorize(), upload.fields([
    { name: 'project_logo', maxCount: 1 },
    { name: 'project_logo_secondary', maxCount: 1 },
    { name: 'project_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('project_logo'), update);
router.put('/:id', authorize(), upload.fields([
    { name: 'project_logo', maxCount: 1 },
    { name: 'project_logo_secondary', maxCount: 1 },
    { name: 'project_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,
//         project_logo: req.file ? req.file.filename : null
//     };

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

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

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

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

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

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

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

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

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

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

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

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

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

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