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

bom_code: {
        type: DataTypes.TEXT,
      
      

      },

revision: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"obsolete"

        ],

      },

effective_from: {
        type: DataTypes.DATE,
      
      

      },

effective_to: {
        type: DataTypes.DATE,
      
      

      },

yield_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

is_default: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

      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_header',
      foreignKey: {
          name: 'bom_headerId',
      },
      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.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

    db.bom_headers.belongsTo(db.users, {
      as: 'approved_by',
      foreignKey: {
        name: 'approved_byId',
      },
      constraints: false,
    });




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

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



  return bom_headers;
};


