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

inspection_number: {
        type: DataTypes.TEXT,
      
      

      },

inspection_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"incoming",


"in_process",


"final",


"audit"

        ],

      },

result: {
        type: DataTypes.ENUM,
      
      

        values: [

"pass",


"fail",


"rework_required",


"pending"

        ],

      },

scheduled_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

findings: {
        type: DataTypes.TEXT,
      
      

      },

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

  quality_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.quality_inspections.hasMany(db.nonconformances, {
      as: 'nonconformances_inspection',
      foreignKey: {
          name: 'inspectionId',
      },
      constraints: false,
    });










//end loop



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

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

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

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



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


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

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



  return quality_inspections;
};


