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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

budget: { type: DataTypes.DECIMAL }, budget_min: {
        type: DataTypes.DECIMAL,
      
      

      },

budget_max: {
        type: DataTypes.DECIMAL,
      
      

      },

budget_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"fixed",


"hourly"

        ],

      },

deadline_at: {
        type: DataTypes.DATE,
      
      

      },

posted_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"Open",


"In-Progress",


"Under_Review",


"Completed",


"Disputed",



        ],

      },

is_remote: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

location_requirement: {
        type: DataTypes.TEXT,
      
      

      },

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

  jobs.associate = (db) => {

    db.jobs.belongsToMany(db.skills, {
      as: 'required_skills',
      foreignKey: {
        name: 'jobs_required_skillsId',
      },
      constraints: false,
      through: 'jobsRequired_skillsSkills',
    });

    db.jobs.belongsToMany(db.skills, {
      as: 'required_skills_filter',
      foreignKey: {
        name: 'jobs_required_skillsId',
      },
      constraints: false,
      through: 'jobsRequired_skillsSkills',
    });

    db.jobs.belongsToMany(db.job_attachments, {
      as: 'attachments',
      foreignKey: {
        name: 'jobs_attachmentsId',
      },
      constraints: false,
      through: 'jobsAttachmentsJob_attachments',
    });

    db.jobs.belongsToMany(db.job_attachments, {
      as: 'attachments_filter',
      foreignKey: {
        name: 'jobs_attachmentsId',
      },
      constraints: false,
      through: 'jobsAttachmentsJob_attachments',
    });


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











    db.jobs.hasMany(db.job_attachments, {
      as: 'job_attachments_job',
      foreignKey: {
          name: 'jobId',
      },
      constraints: false,
    });


    db.jobs.hasMany(db.proposals, {
      as: 'proposals_job',
      foreignKey: {
          name: 'jobId',
      },
      constraints: false,
    });


    db.jobs.hasMany(db.contracts, {
      as: 'contracts_job',
      foreignKey: {
          name: 'jobId',
      },
      constraints: false,
    });
















//end loop



    db.jobs.belongsTo(db.users, {
      as: 'client',
      foreignKey: {
        name: 'clientId',
      },
      constraints: false,
    });

    db.jobs.belongsTo(db.service_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });




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

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



  return jobs;
};


