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

subject: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"under_review",


"resolved",


"rejected"

        ],

      },

resolution: {
        type: DataTypes.ENUM,
      
      

        values: [

"refund_client",


"pay_freelancer",


"split",


"no_action"

        ],

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  disputes.associate = (db) => {


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




























//end loop



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

    db.disputes.belongsTo(db.users, {
      as: 'opened_by',
      foreignKey: {
        name: 'opened_byId',
      },
      constraints: false,
    });



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


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

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



  return disputes;
};


