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

period_start_at: {
        type: DataTypes.DATE,
      
      

      },

period_end_at: {
        type: DataTypes.DATE,
      
      

      },

report_title: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

share_scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"family",


"clinicians",


"admin"

        ],

      },

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

  clinical_reports.associate = (db) => {


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





























//end loop



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

    db.clinical_reports.belongsTo(db.users, {
      as: 'generated_by_user',
      foreignKey: {
        name: 'generated_by_userId',
      },
      constraints: false,
    });

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



    db.clinical_reports.hasMany(db.file, {
      as: 'report_file',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.clinical_reports.getTableName(),
        belongsToColumn: 'report_file',
      },
    });


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

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



  return clinical_reports;
};


