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,
      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"under_review",


"resolved"

        ],

      },

resolution: {
        type: DataTypes.ENUM,
      
      

        values: [

"none",


"tutor_favor",


"learner_favor",


"split"

        ],

      },

admin_notes: {
        type: DataTypes.TEXT,
      
      

      },

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.bookings, {
      as: 'booking',
      foreignKey: {
        name: 'bookingId',
      },
      constraints: false,
    });

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

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



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

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


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

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



  return disputes;
};


