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,
      },

device_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"phone",


"tablet",


"laptop",


"other"

        ],

      },

brand: {
        type: DataTypes.TEXT,
      
      

      },

model: {
        type: DataTypes.TEXT,
      
      

      },

color: {
        type: DataTypes.TEXT,
      
      

      },

serial_number: {
        type: DataTypes.TEXT,
      
      

      },

accessories_received: {
        type: DataTypes.TEXT,
      
      

      },

device_condition: {
        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.repair_orders, {
      as: 'repair_orders_device',
      foreignKey: {
          name: 'deviceId',
      },
      constraints: false,
    });













//end loop



    db.devices.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });




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

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



  return devices;
};


