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

submission_channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"payer_portal",


"phone",


"fax",


"email",


"clearinghouse",


"other"

        ],

      },

reference_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"pending",


"additional_info_requested",


"approved",


"denied",


"withdrawn",


"closed"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

decision_at: {
        type: DataTypes.DATE,
      
      

      },

decision_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"unknown",


"approved",


"denied",


"partial",


"pended"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  submissions.associate = (db) => {


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
















    db.submissions.hasMany(db.submission_events, {
      as: 'submission_events_submission',
      foreignKey: {
          name: 'submissionId',
      },
      constraints: false,
    });



    db.submissions.hasMany(db.denials, {
      as: 'denials_submission',
      foreignKey: {
          name: 'submissionId',
      },
      constraints: false,
    });













//end loop



    db.submissions.belongsTo(db.authorization_cases, {
      as: 'authorization_case',
      foreignKey: {
        name: 'authorization_caseId',
      },
      constraints: false,
    });

    db.submissions.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return submissions;
};


