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 bom_headers = sequelize.define(
    'bom_headers',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

bom_number: {
        type: DataTypes.TEXT,
      
      

      },

revision: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"released",


"obsolete"

        ],

      },

effective_from: {
        type: DataTypes.DATE,
      
      

      },

effective_to: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  bom_headers.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity











    db.bom_headers.hasMany(db.bom_lines, {
      as: 'bom_lines_bom',
      foreignKey: {
          name: 'bomId',
      },
      constraints: false,
    });














    db.bom_headers.hasMany(db.production_orders, {
      as: 'production_orders_bom',
      foreignKey: {
          name: 'bomId',
      },
      constraints: false,
    });



















//end loop



    db.bom_headers.belongsTo(db.materials, {
      as: 'parent_material',
      foreignKey: {
        name: 'parent_materialId',
      },
      constraints: false,
    });

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




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

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



  return bom_headers;
};


