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

stage: {
        type: DataTypes.ENUM,
      
      

        values: [

"intake",


"needs_evidence",


"ready_to_submit",


"submitted",


"pending_decision",


"approved",


"denied",


"appeal_in_progress",


"closed"

        ],

      },

urgency: {
        type: DataTypes.ENUM,
      
      

        values: [

"routine",


"urgent",


"stat"

        ],

      },

ordering_provider_name: {
        type: DataTypes.TEXT,
      
      

      },

rendering_provider_name: {
        type: DataTypes.TEXT,
      
      

      },

site_of_service: {
        type: DataTypes.TEXT,
      
      

      },

scheduled_date: {
        type: DataTypes.DATE,
      
      

      },

dollars_at_risk: {
        type: DataTypes.DECIMAL,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

sla_state: {
        type: DataTypes.ENUM,
      
      

        values: [

"on_track",


"due_soon",


"overdue"

        ],

      },

internal_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  authorization_cases.associate = (db) => {


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














    db.authorization_cases.hasMany(db.case_documents, {
      as: 'case_documents_authorization_case',
      foreignKey: {
          name: 'authorization_caseId',
      },
      constraints: false,
    });


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



    db.authorization_cases.hasMany(db.tasks, {
      as: 'tasks_authorization_case',
      foreignKey: {
          name: 'authorization_caseId',
      },
      constraints: false,
    });


    db.authorization_cases.hasMany(db.denials, {
      as: 'denials_authorization_case',
      foreignKey: {
          name: 'authorization_caseId',
      },
      constraints: false,
    });






    db.authorization_cases.hasMany(db.ai_jobs, {
      as: 'ai_jobs_authorization_case',
      foreignKey: {
          name: 'authorization_caseId',
      },
      constraints: false,
    });



    db.authorization_cases.hasMany(db.review_actions, {
      as: 'review_actions_authorization_case',
      foreignKey: {
          name: 'authorization_caseId',
      },
      constraints: false,
    });






//end loop



    db.authorization_cases.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

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

    db.authorization_cases.belongsTo(db.payer_plans, {
      as: 'payer_plan',
      foreignKey: {
        name: 'payer_planId',
      },
      constraints: false,
    });

    db.authorization_cases.belongsTo(db.cpt_codes, {
      as: 'cpt_code',
      foreignKey: {
        name: 'cpt_codeId',
      },
      constraints: false,
    });

    db.authorization_cases.belongsTo(db.procedure_families, {
      as: 'procedure_family',
      foreignKey: {
        name: 'procedure_familyId',
      },
      constraints: false,
    });

    db.authorization_cases.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });




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

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



  return authorization_cases;
};


