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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"paused",


"completed",


"cancelled"

        ],

      },

agreed_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

pricing_model: {
        type: DataTypes.ENUM,
      
      

        values: [

"fixed",


"hourly"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

terms: {
        type: DataTypes.TEXT,
      
      

      },

revision_limit: {
        type: DataTypes.INTEGER,
      
      

      },

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

  contracts.associate = (db) => {


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














    db.contracts.hasMany(db.milestones, {
      as: 'milestones_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });


    db.contracts.hasMany(db.deliverables, {
      as: 'deliverables_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });


    db.contracts.hasMany(db.conversations, {
      as: 'conversations_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });




    db.contracts.hasMany(db.payments, {
      as: 'payments_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });



    db.contracts.hasMany(db.reviews, {
      as: 'reviews_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });


    db.contracts.hasMany(db.disputes, {
      as: 'disputes_contract',
      foreignKey: {
          name: 'contractId',
      },
      constraints: false,
    });







//end loop



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

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

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

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




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

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



  return contracts;
};


