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

lab_reference: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"in_progress",


"completed",


"reviewed",


"approved",


"rejected"

        ],

      },

date_submitted: {
        type: DataTypes.DATE,
      
      

      },

date_completed: {
        type: DataTypes.DATE,
      
      

      },

data_source: {
        type: DataTypes.ENUM,
      
      

        values: [

"manual",


"csv_import",


"lims_import",


"api"

        ],

      },

parameters_json: {
        type: DataTypes.TEXT,
      
      

      },

results_json: {
        type: DataTypes.TEXT,
      
      

      },

qaqc_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"passed",


"failed",


"needs_review"

        ],

      },

qaqc_notes: {
        type: DataTypes.TEXT,
      
      

      },

reviewed_at: {
        type: DataTypes.DATE,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

is_deleted: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  metallurgical_tests.associate = (db) => {


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















    db.metallurgical_tests.hasMany(db.test_result_points, {
      as: 'test_result_points_metallurgical_test',
      foreignKey: {
          name: 'metallurgical_testId',
      },
      constraints: false,
    });




















//end loop



    db.metallurgical_tests.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.metallurgical_tests.belongsTo(db.project_phases, {
      as: 'phase',
      foreignKey: {
        name: 'phaseId',
      },
      constraints: false,
    });

    db.metallurgical_tests.belongsTo(db.samples, {
      as: 'sample',
      foreignKey: {
        name: 'sampleId',
      },
      constraints: false,
    });

    db.metallurgical_tests.belongsTo(db.laboratories, {
      as: 'laboratory',
      foreignKey: {
        name: 'laboratoryId',
      },
      constraints: false,
    });

    db.metallurgical_tests.belongsTo(db.test_type_schemas, {
      as: 'test_type_schema',
      foreignKey: {
        name: 'test_type_schemaId',
      },
      constraints: false,
    });

    db.metallurgical_tests.belongsTo(db.users, {
      as: 'entered_by',
      foreignKey: {
        name: 'entered_byId',
      },
      constraints: false,
    });

    db.metallurgical_tests.belongsTo(db.users, {
      as: 'reviewed_by',
      foreignKey: {
        name: 'reviewed_byId',
      },
      constraints: false,
    });

    db.metallurgical_tests.belongsTo(db.users, {
      as: 'approved_by',
      foreignKey: {
        name: 'approved_byId',
      },
      constraints: false,
    });

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




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

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



  return metallurgical_tests;
};


