text: "use strict";
  require("dotenv").config();
  var dayjs = require("dayjs");
  var relativeTime = require("dayjs/plugin/relativeTime");
  dayjs.extend(relativeTime);
  
  const Device = require("../models/device");
  const moment = require("moment");
  const { data } = require("jquery");
  
  let controller = {
    devices: async (req, res, next) => {
      var devices = await Device.find();
      const fs = require("fs");
      const body = fs.readFileSync(
        process.cwd() + "/backend/views/devices.hbs",
        "utf8"
      );
      return res.status(200).json({
        body: body,
        script: "/public/js/devices.js",
        devices: devices,
        user: req.user,
      });
    },
  
    updateDevice: async (req, res, next) => {
      //console.log(req.body);
      await Device.updateOne(
        { mac: req.body.key },
        {
          id: req.body.id || "",
          mac: req.body.mac,
          name: req.body.name,
          reference: req.body.reference || "",
          serial_number: req.body.serial_number || "",
          manufacturer: req.body.manufacturer || "",
          description: req.body.description || "",
          avatar: req.body.avatar || "",
          type: req.body.type || 0,
          //state: req.body.state || false,
          //online: req.body.online || false,
          pin: req.body.pin || 0,
        }
      )
        .then((device) => {
          req.body["key"] = req.body.mac;
          return res.status(201).json(req.body);
        })
        .catch((err) => {
          console.log(err);
          return res
            .status(500)
            .json({ general: "Error al guardar los datos", err: err });
        });
    },
  
    createDevice: async (req, res, next) => {
      Device.findOne({ mac: req.body.mac })
        .then((data) => {
          //si la consulta se ejecuta
          if (data) {
            throw new Error("El dispositivo ya está registrado!");
          } else {
            //si no existe el dispositivo se registra
            let device = new Device({
              id: req.body.id || "",
              mac: req.body.mac,
              name: req.body.name,
              reference: req.body.reference || "",
              serial_number: req.body.serial_number || "",
              manufacturer: req.body.manufacturer || "",
              description: req.body.description || "",
              avatar: req.body.avatar || "",
              type: req.body.type || 0,
              state: req.body.state || false,
              online: req.body.online || false,
              pin: req.body.pin || 0,
            });
            return device.save();
          }
        })
        .then((device) => {
          req.body["key"] = req.body.mac;
          return res.status(201).json(req.body);
        })
        .catch((err) => {
          return res.status(500).json({ general: "Error al guardar los datos", error: err });
        });
    },
  
    deleteDevice: async (req, res, next) => {
      await Device.deleteOne({ mac: req.params.id })
        .then(async (data) => {
          var devices = await Device.find();
          /*const fs = require("fs");
          const body = fs.readFileSync(
            process.cwd() + "/backend/views/devices.hbs",
            "utf8"
          );*/
          return res.status(200).json({
            /*body: body,*/
            devices: devices,
            user: req.user,
          });
        })
        .catch((error) => {
          return res.status(500).json({ general: error });
        });
    },
  };
  
  module.exports = controller;
  