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

nc_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"contained",


"dispositioned",


"closed",


"rejected"

        ],

      },

nonconformance_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"product",


"process",


"documentation",


"supplier",


"training",


"other"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

disposition: {
        type: DataTypes.ENUM,
      
      

        values: [

"use_as_is",


"rework",


"repair",


"scrap",


"return_to_supplier",


"other"

        ],

      },

detected_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.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.batches, {
      as: 'batch',
      foreignKey: {
        name: 'batchId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      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;
};


