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

name: {
        type: DataTypes.TEXT,
      
      

      },

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

  accounts.associate = (db) => {


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


    db.accounts.hasMany(db.users, {
      as: 'users_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });





    db.accounts.hasMany(db.patients, {
      as: 'patients_account',
      foreignKey: {
          name: 'accountId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.patient_access_grants, {
      as: 'patient_access_grants_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.medications, {
      as: 'medications_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.medication_schedules, {
      as: 'medication_schedules_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


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


    db.accounts.hasMany(db.reminders, {
      as: 'reminders_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.cognitive_programs, {
      as: 'cognitive_programs_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.cognitive_exercises, {
      as: 'cognitive_exercises_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.cognitive_sessions, {
      as: 'cognitive_sessions_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.cognitive_attempts, {
      as: 'cognitive_attempts_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.stage_assessments, {
      as: 'stage_assessments_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.voice_assistant_sessions, {
      as: 'voice_assistant_sessions_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.iot_devices, {
      as: 'iot_devices_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.iot_telemetry_events, {
      as: 'iot_telemetry_events_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.geofences, {
      as: 'geofences_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.alerts, {
      as: 'alerts_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.alert_notifications, {
      as: 'alert_notifications_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.shared_dashboard_posts, {
      as: 'shared_dashboard_posts_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.scientific_articles, {
      as: 'scientific_articles_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.article_recommendations, {
      as: 'article_recommendations_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.clinical_reports, {
      as: 'clinical_reports_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });


    db.accounts.hasMany(db.admin_metrics_snapshots, {
      as: 'admin_metrics_snapshots_accounts',
      foreignKey: {
          name: 'accountsId',
      },
      constraints: false,
    });



//end loop






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

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



  return accounts;
};


