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

disposition_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"use_as_is",


"rework",


"repair",


"scrap",


"return_to_supplier",


"sort"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"proposed",


"approved",


"implemented",


"verified"

        ],

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

  dispositions.associate = (db) => {


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










































//end loop



    db.dispositions.belongsTo(db.nonconformances, {
      as: 'nonconformance',
      foreignKey: {
        name: 'nonconformanceId',
      },
      constraints: false,
    });

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

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



    db.dispositions.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.dispositions.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return dispositions;
};


