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

cover_letter: {
        type: DataTypes.TEXT,
      
      

      },

bid_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

pricing_model: {
        type: DataTypes.ENUM,
      
      

        values: [

"fixed",


"hourly"

        ],

      },

estimated_days: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"Pending",
"Accepted",
"Rejected"









        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

responded_at: {
        type: DataTypes.DATE,
      
      

      },

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

  proposals.associate = (db) => {


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













    db.proposals.hasMany(db.contracts, {
      as: 'contracts_proposal',
      foreignKey: {
          name: 'proposalId',
      },
      constraints: false,
    });
















//end loop



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

    db.proposals.belongsTo(db.users, {
      as: 'freelancer',
      foreignKey: {
        name: 'freelancerId',
      },
      constraints: false,
    });



    db.proposals.hasMany(db.file, {
      as: 'sample_files',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.proposals.getTableName(),
        belongsToColumn: 'sample_files',
      },
    });


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

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



  return proposals;
};


