const express = require("express");
const router = express.Router();
const multer = require("multer");
const authorize = require("../../../../middlewares/authorize-sapps");
const projectManagementService = require("./projectManagement.service");
const path = require("path");

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./public/data/documents");
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + "-" + file.originalname);
  },
});

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

// routes
router.post(
  "/create",
  authorize(),
  upload.fields([
    { name: "nda_document", maxCount: 1 },
    { name: "da_document", maxCount: 1 },
    { name: "pks_document", maxCount: 1 },
  ]),
  create
);
router.get("/", authorize(), getAll);
router.get("/filter", authorize(), filter);
router.get("/option", authorize(), option);
router.get("/count", authorize(), count);
router.put(
  "/:id",
  authorize(),
  upload.fields([
    { name: "nda_document", maxCount: 1 },
    { name: "da_document", maxCount: 1 },
    { name: "pks_document", maxCount: 1 },
  ]),
  update
);
router.put("/enabled/:id", authorize(), enabled);
router.delete("/:id", authorize(), _delete);

function create(req, res, next) {
  const projectData = {
    ...req.body,
    nda_document: req.files["nda_document"] ? req.files["nda_document"][0].filename : null,  
    da_document: req.files["da_document"] ? req.files["da_document"][0].filename : null,  
    pks_document: req.files["pks_document"] ? req.files["pks_document"][0].filename : null,
  };

  projectManagementService
    .create(projectData)
    .then((result) => res.json(result))
    .catch(next);
}

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

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

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

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

function update(req, res, next) {
  const baseUrl = "http://localhost:3000/data/documents/";
  const files = req.files;
  const updateData = {
    ...req.body,
    nda_document: files["nda_document"]
      ? baseUrl + files["nda_document"][0].filename
      : null,
    da_document: files["da_document"]
      ? baseUrl + files["da_document"][0].filename
      : null,
    pks_document: files["pks_document"]
      ? baseUrl + files["pks_document"][0].filename
      : null,
  };

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

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

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

module.exports = router;
