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

product_name: {
        type: DataTypes.TEXT,
      
      

      },

product_code: {
        type: DataTypes.TEXT,
      
      

      },

intended_use: {
        type: DataTypes.TEXT,
      
      

      },

risk_classification: {
        type: DataTypes.ENUM,
      
      

        values: [

"eu_class_i",


"eu_class_iia",


"eu_class_iib",


"eu_class_iii",


"us_class_i",


"us_class_ii",


"us_class_iii",


"other"

        ],

      },

udi_di: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  products.associate = (db) => {


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




















    db.products.hasMany(db.batches, {
      as: 'batches_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.device_master_records, {
      as: 'device_master_records_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.risk_assessments, {
      as: 'risk_assessments_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });




    db.products.hasMany(db.nonconformances, {
      as: 'nonconformances_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.complaints, {
      as: 'complaints_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });





    db.products.hasMany(db.regulatory_submissions, {
      as: 'regulatory_submissions_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });




//end loop





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


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

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



  return products;
};


