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

job_title: {
        type: DataTypes.TEXT,
      
      

      },

employment_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"full_time",


"part_time",


"contract",


"internship",


"freelance"

        ],

      },

work_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"on_site",


"hybrid",


"remote"

        ],

      },

location_text: {
        type: DataTypes.TEXT,
      
      

      },

salary_min: {
        type: DataTypes.DECIMAL,
      
      

      },

salary_max: {
        type: DataTypes.DECIMAL,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"open",


"closed",


"paused"

        ],

      },

posted_at: {
        type: DataTypes.DATE,
      
      

      },

closes_at: {
        type: DataTypes.DATE,
      
      

      },

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

  job_listings.associate = (db) => {


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
































    db.job_listings.hasMany(db.job_applications, {
      as: 'job_applications_job_listing',
      foreignKey: {
          name: 'job_listingId',
      },
      constraints: false,
    });













//end loop



    db.job_listings.belongsTo(db.job_companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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




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

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



  return job_listings;
};


