const express = require("express");
const router = express.Router();
const authorize = require("../../../../middlewares/authorize-sapps");
const assetService = require("./asset.service");
const assetQRService = require("./assetQR.service");
const assetImportService = require("./assetImport.service");

router.post("/create", authorize(), (req, res, next) => assetService.create(req.body).then((r) => res.json(r)).catch(next));
router.post("/import", authorize(), (req, res, next) => assetImportService.importAssets({ ...req.body, user_id: req.auth.id }).then((r) => res.json(r)).catch(next));
router.post("/", authorize(), (req, res, next) => assetService.getAll(req.body).then((r) => res.json(r)).catch(next));
router.post("/option", authorize(), (req, res, next) => assetService.option(req.body).then((r) => res.json(r)).catch(next));
router.get("/count", authorize(), (req, res, next) => assetService.count(req.query).then((r) => res.json(r)).catch(next));
router.get("/qr/:id", authorize(), (req, res, next) => assetQRService.generateAssetQR(req.params.id, req.headers.origin).then((r) => res.json({ success: true, qr: r })).catch(next));
router.get("/:id", authorize(), (req, res, next) => assetService.getById(req.params.id).then((r) => res.json(r)).catch(next));
router.put("/:id", authorize(), (req, res, next) => assetService.update(req.params.id, req.body).then((r) => res.json(r)).catch(next));
router.put("/enabled/:id", authorize(), (req, res, next) => assetService.enabled(req.params.id, req.body).then((r) => res.json(r)).catch(next));
router.delete("/:id", authorize(), (req, res, next) => assetService.delete(req.params.id).then((r) => res.json(r)).catch(next));

// Specialized routes
router.post("/checkout/:id", authorize(), (req, res, next) => assetService.checkout(req.params.id, req.body).then((r) => res.json(r)).catch(next));
router.post("/checkin/:id", authorize(), (req, res, next) => assetService.checkin(req.params.id, req.body).then((r) => res.json(r)).catch(next));
router.post("/maintenance", authorize(), (req, res, next) => assetService.maintenance(req.body).then((r) => res.json(r)).catch(next));

module.exports = router;
