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

case_number: {
        type: DataTypes.TEXT,
      
      

      },

scheduled_start: {
        type: DataTypes.DATE,
      
      

      },

scheduled_end: {
        type: DataTypes.DATE,
      
      

      },

surgical_details: {
        type: DataTypes.TEXT,
      
      

      },

price: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"scheduled",


"in_progress",


"completed",


"cancelled"

        ],

      },

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

  surgery_cases.associate = (db) => {

    db.surgery_cases.belongsToMany(db.surgery_consumables, {
      as: 'consumables',
      foreignKey: {
        name: 'surgery_cases_consumablesId',
      },
      constraints: false,
      through: 'surgery_casesConsumablesSurgery_consumables',
    });

    db.surgery_cases.belongsToMany(db.surgery_consumables, {
      as: 'consumables_filter',
      foreignKey: {
        name: 'surgery_cases_consumablesId',
      },
      constraints: false,
      through: 'surgery_casesConsumablesSurgery_consumables',
    });


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



















    db.surgery_cases.hasMany(db.surgery_consumables, {
      as: 'surgery_consumables_surgery_case',
      foreignKey: {
          name: 'surgery_caseId',
      },
      constraints: false,
    });


    db.surgery_cases.hasMany(db.anesthesia_records, {
      as: 'anesthesia_records_surgery_case',
      foreignKey: {
          name: 'surgery_caseId',
      },
      constraints: false,
    });



























//end loop



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

    db.surgery_cases.belongsTo(db.doctors, {
      as: 'surgeon',
      foreignKey: {
        name: 'surgeonId',
      },
      constraints: false,
    });




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

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



  return surgery_cases;
};


