const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function (sequelize, DataTypes) {
  const devices = sequelize.define(
    'devices',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

      type: {
        type: DataTypes.ENUM,

        values: ['Laptop', 'Desktop', 'Tablet'],
      },

      platform: {
        type: DataTypes.ENUM,

        values: ['Windows', 'MacOS', 'Linux'],
      },

      serial: {
        type: DataTypes.TEXT,
      },

      asset_tag: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['Assigned', 'InStock', 'Retired'],
      },

      encryption: {
        type: DataTypes.BOOLEAN,

        allowNull: false,
        defaultValue: false,
      },

      os_version: {
        type: DataTypes.TEXT,
      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  devices.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.devices.hasMany(db.inventory_events, {
      as: 'inventory_events_device',
      foreignKey: {
        name: 'deviceId',
      },
      constraints: false,
    });

    //end loop

    db.devices.belongsTo(db.employees, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

    db.devices.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });

    db.devices.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.devices.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };

  return devices;
};
