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

inspection_number: {
        type: DataTypes.TEXT,
      
      

      },

context: {
        type: DataTypes.ENUM,
      
      

        values: [

"receiving",


"in_process",


"final"

        ],

      },

inspected_at: {
        type: DataTypes.DATE,
      
      

      },

result: {
        type: DataTypes.ENUM,
      
      

        values: [

"pass",


"fail",


"conditional_pass"

        ],

      },

sample_size: {
        type: DataTypes.DECIMAL,
      
      

      },

defect_count: {
        type: DataTypes.DECIMAL,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

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

  qa_inspections.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity



























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


















//end loop



    db.qa_inspections.belongsTo(db.qa_inspection_plans, {
      as: 'plan',
      foreignKey: {
        name: 'planId',
      },
      constraints: false,
    });

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

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

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

    db.qa_inspections.belongsTo(db.production_operations, {
      as: 'operation',
      foreignKey: {
        name: 'operationId',
      },
      constraints: false,
    });

    db.qa_inspections.belongsTo(db.users, {
      as: 'inspector',
      foreignKey: {
        name: 'inspectorId',
      },
      constraints: false,
    });



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


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

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



  return qa_inspections;
};


