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

first_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

sex: {
        type: DataTypes.ENUM,
      
      

        values: [

"unknown",


"female",


"male",


"other"

        ],

      },

mrn: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

address_line_1: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

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

  patients.associate = (db) => {


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













    db.patients.hasMany(db.authorization_cases, {
      as: 'authorization_cases_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


















//end loop



    db.patients.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return patients;
};


