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

sample_code: {
        type: DataTypes.TEXT,
      
      

      },

sample_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"drill_core",


"rc_chip",


"composite",


"bulk",


"variability",


"metallurgical",


"environmental",


"other"

        ],

      },

drillhole_id: {
        type: DataTypes.TEXT,
      
      

      },

depth_from_m: {
        type: DataTypes.DECIMAL,
      
      

      },

depth_to_m: {
        type: DataTypes.DECIMAL,
      
      

      },

collection_date: {
        type: DataTypes.DATE,
      
      

      },

lithology: {
        type: DataTypes.TEXT,
      
      

      },

alteration: {
        type: DataTypes.TEXT,
      
      

      },

geomet_domain: {
        type: DataTypes.TEXT,
      
      

      },

prep_protocol: {
        type: DataTypes.TEXT,
      
      

      },

qaqc_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"none",


"blank",


"standard",


"duplicate",


"umpire"

        ],

      },

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

  samples.associate = (db) => {


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











    db.samples.hasMany(db.chain_of_custody_events, {
      as: 'chain_of_custody_events_sample',
      foreignKey: {
          name: 'sampleId',
      },
      constraints: false,
    });




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





















//end loop



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

    db.samples.belongsTo(db.ore_bodies, {
      as: 'ore_body',
      foreignKey: {
        name: 'ore_bodyId',
      },
      constraints: false,
    });

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



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


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

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



  return samples;
};


