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

order_number: {
        type: DataTypes.TEXT,
      
      

      },

order_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"lab",


"radiology",


"ct",


"endoscopy",


"ecg",


"other"

        ],

      },

subtype: {
        type: DataTypes.TEXT,
      
      

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"in_progress",


"completed",


"cancelled"

        ],

      },

created_datetime: {
        type: DataTypes.DATE,
      
      

      },

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

  orders.associate = (db) => {


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











    db.orders.hasMany(db.radiology_studies, {
      as: 'radiology_studies_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });



    db.orders.hasMany(db.ct_studies, {
      as: 'ct_studies_order',
      foreignKey: {
          name: 'orderId',
      },
      constraints: false,
    });



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
































//end loop



    db.orders.belongsTo(db.visits, {
      as: 'visit',
      foreignKey: {
        name: 'visitId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.patients, {
      as: 'patient',
      foreignKey: {
        name: 'patientId',
      },
      constraints: false,
    });

    db.orders.belongsTo(db.doctors, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });




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

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



  return orders;
};


