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 inspections = sequelize.define(
    '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"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

result: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"pass",


"fail",


"conditional_pass"

        ],

      },

sample_size: {
        type: DataTypes.DECIMAL,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

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

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



    db.inspections.hasMany(db.alerts, {
      as: 'alerts_related_inspection',
      foreignKey: {
          name: 'related_inspectionId',
      },
      constraints: false,
    });




//end loop



    db.inspections.belongsTo(db.inspection_plans, {
      as: 'inspection_plan',
      foreignKey: {
        name: 'inspection_planId',
      },
      constraints: false,
    });

    db.inspections.belongsTo(db.materials, {
      as: 'material',
      foreignKey: {
        name: 'materialId',
      },
      constraints: false,
    });

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

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

    db.inspections.belongsTo(db.work_order_operations, {
      as: 'work_order_operation',
      foreignKey: {
        name: 'work_order_operationId',
      },
      constraints: false,
    });

    db.inspections.belongsTo(db.users, {
      as: 'inspector_user',
      foreignKey: {
        name: 'inspector_userId',
      },
      constraints: false,
    });



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


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

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



  return inspections;
};


