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_return",


"audit"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"minor",


"major",


"critical"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"contained",


"in_review",


"dispositioned",


"closed"

        ],

      },

reported_at: {
        type: DataTypes.DATE,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

containment_action: {
        type: DataTypes.TEXT,
      
      

      },

      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

































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


    db.nonconformances.hasMany(db.corrective_actions, {
      as: 'corrective_actions_nonconformance',
      foreignKey: {
          name: 'nonconformanceId',
      },
      constraints: false,
    });









//end loop



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

    db.nonconformances.belongsTo(db.lots, {
      as: 'lot',
      foreignKey: {
        name: 'lotId',
      },
      constraints: false,
    });

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

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

    db.nonconformances.belongsTo(db.machines, {
      as: 'machine',
      foreignKey: {
        name: 'machineId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.users, {
      as: 'reported_by',
      foreignKey: {
        name: 'reported_byId',
      },
      constraints: false,
    });

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



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


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

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



  return nonconformances;
};


