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


"under_investigation",


"containment",


"capa_required",


"closed",


"void"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

containment_action: {
        type: DataTypes.TEXT,
      
      

      },

disposition: {
        type: DataTypes.ENUM,
      
      

        values: [

"use_as_is",


"rework",


"scrap",


"return_to_supplier",


"sort"

        ],

      },

reported_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





























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





//end loop



    db.nonconformances.belongsTo(db.plants, {
      as: 'plant',
      foreignKey: {
        name: 'plantId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.qa_inspections, {
      as: 'inspection',
      foreignKey: {
        name: 'inspectionId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.work_orders, {
      as: 'work_order',
      foreignKey: {
        name: 'work_orderId',
      },
      constraints: false,
    });

    db.nonconformances.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

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

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

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


