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


"internal_audit"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"minor",


"major",


"critical"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"under_review",


"containment",


"capa_required",


"closed",


"void"

        ],

      },

reported_at: {
        type: DataTypes.DATE,
      
      

      },

problem_description: {
        type: DataTypes.TEXT,
      
      

      },

containment_actions: {
        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.capa_actions, {
      as: 'capa_actions_nonconformance',
      foreignKey: {
          name: 'nonconformanceId',
      },
      constraints: false,
    });




//end loop



    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.serials, {
      as: 'serial',
      foreignKey: {
        name: 'serialId',
      },
      constraints: false,
    });

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

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

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

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


