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

job_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"document_classification",


"field_extraction",


"denial_reason_extraction",


"appeal_draft_generation",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"succeeded",


"failed",


"canceled"

        ],

      },

prompt_text: {
        type: DataTypes.TEXT,
      
      

      },

model_ref: {
        type: DataTypes.TEXT,
      
      

      },

input_json: {
        type: DataTypes.TEXT,
      
      

      },

queued_at: {
        type: DataTypes.DATE,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  ai_jobs.associate = (db) => {


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
























    db.ai_jobs.hasMany(db.ai_outputs, {
      as: 'ai_outputs_ai_job',
      foreignKey: {
          name: 'ai_jobId',
      },
      constraints: false,
    });







//end loop



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

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

    db.ai_jobs.belongsTo(db.case_documents, {
      as: 'case_document',
      foreignKey: {
        name: 'case_documentId',
      },
      constraints: false,
    });




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

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



  return ai_jobs;
};


