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

coa_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"issued",


"superseded",


"void"

        ],

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

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

  certificates_of_analysis.associate = (db) => {


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


































//end loop



    db.certificates_of_analysis.belongsTo(db.batches, {
      as: 'batch',
      foreignKey: {
        name: 'batchId',
      },
      constraints: false,
    });

    db.certificates_of_analysis.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

    db.certificates_of_analysis.belongsTo(db.users, {
      as: 'issued_by',
      foreignKey: {
        name: 'issued_byId',
      },
      constraints: false,
    });



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


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

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



  return certificates_of_analysis;
};


