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

event_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"medication",


"appointment",


"cognitive_session",


"care_task",


"family_note",


"other"

        ],

      },

title_text: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

location_text: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"scheduled",


"completed",


"cancelled"

        ],

      },

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

      },

reminder_minutes_before: {
        type: DataTypes.INTEGER,
      
      

      },

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

  calendar_events.associate = (db) => {


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











    db.calendar_events.hasMany(db.reminders, {
      as: 'reminders_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });



















//end loop



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

    db.calendar_events.belongsTo(db.users, {
      as: 'created_by_user',
      foreignKey: {
        name: 'created_by_userId',
      },
      constraints: false,
    });

    db.calendar_events.belongsTo(db.medications, {
      as: 'linked_medication',
      foreignKey: {
        name: 'linked_medicationId',
      },
      constraints: false,
    });

    db.calendar_events.belongsTo(db.accounts, {
      as: 'accounts',
      foreignKey: {
        name: 'accountsId',
      },
      constraints: false,
    });




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

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



  return calendar_events;
};


