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

lab_order_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"in_progress",


"completed"

        ],

      },

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

  lab_orders.associate = (db) => {

    db.lab_orders.belongsToMany(db.lab_tests, {
      as: 'tests',
      foreignKey: {
        name: 'lab_orders_testsId',
      },
      constraints: false,
      through: 'lab_ordersTestsLab_tests',
    });

    db.lab_orders.belongsToMany(db.lab_tests, {
      as: 'tests_filter',
      foreignKey: {
        name: 'lab_orders_testsId',
      },
      constraints: false,
      through: 'lab_ordersTestsLab_tests',
    });


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
















    db.lab_orders.hasMany(db.lab_results, {
      as: 'lab_results_lab_order',
      foreignKey: {
          name: 'lab_orderId',
      },
      constraints: false,
    });































//end loop



    db.lab_orders.belongsTo(db.orders, {
      as: 'order',
      foreignKey: {
        name: 'orderId',
      },
      constraints: false,
    });

    db.lab_orders.belongsTo(db.users, {
      as: 'lab_technician',
      foreignKey: {
        name: 'lab_technicianId',
      },
      constraints: false,
    });




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

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



  return lab_orders;
};


