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

appeal_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"first_level",


"second_level",


"peer_to_peer",


"external_review",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_review",


"submitted",


"pending",


"approved",


"denied",


"withdrawn",


"closed"

        ],

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

decision_at: {
        type: DataTypes.DATE,
      
      

      },

reviewed_at: {
        type: DataTypes.DATE,
      
      

      },

appeal_summary: {
        type: DataTypes.TEXT,
      
      

      },

export_artifact_key: {
        type: DataTypes.TEXT,
      
      

      },

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

  appeals.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.appeals.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.appeals.belongsTo(db.locations, {
      as: 'location',
      foreignKey: {
        name: 'locationId',
      },
      constraints: false,
    });

    db.appeals.belongsTo(db.denials, {
      as: 'denial',
      foreignKey: {
        name: 'denialId',
      },
      constraints: false,
    });

    db.appeals.belongsTo(db.templates, {
      as: 'template',
      foreignKey: {
        name: 'templateId',
      },
      constraints: false,
    });

    db.appeals.belongsTo(db.users, {
      as: 'reviewed_by_user',
      foreignKey: {
        name: 'reviewed_by_userId',
      },
      constraints: false,
    });




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

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



  return appeals;
};


