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

ncr_number: {
        type: DataTypes.TEXT,
      
      

      },

source: {
        type: DataTypes.ENUM,
      
      

        values: [

"incoming",


"in_process",


"final",


"customer",


"audit"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"minor",


"major",


"critical"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"contained",


"investigating",


"closed",


"void"

        ],

      },

reported_at: {
        type: DataTypes.DATE,
      
      

      },

problem_statement: {
        type: DataTypes.TEXT,
      
      

      },

containment_action: {
        type: DataTypes.TEXT,
      
      

      },

root_cause: {
        type: DataTypes.TEXT,
      
      

      },

corrective_action: {
        type: DataTypes.TEXT,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  nonconformances.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.nonconformances.belongsTo(db.users, {
      as: 'reported_by',
      foreignKey: {
        name: 'reported_byId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.materials, {
      as: 'material',
      foreignKey: {
        name: 'materialId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.inventory_lots, {
      as: 'inventory_lot',
      foreignKey: {
        name: 'inventory_lotId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.production_orders, {
      as: 'production_order',
      foreignKey: {
        name: 'production_orderId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.production_lots, {
      as: 'production_lot',
      foreignKey: {
        name: 'production_lotId',
      },
      constraints: false,
    });



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


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

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



  return nonconformances;
};


