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

return_number: {
        type: DataTypes.TEXT,
      
      

      },

returned_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"requested",


"approved",


"rejected",


"completed"

        ],

      },

reason: {
        type: DataTypes.ENUM,
      
      

        values: [

"defective",


"wrong_item",


"no_longer_needed",


"exchange",


"other"

        ],

      },

refund_total: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  returns.associate = (db) => {


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




















    db.returns.hasMany(db.return_items, {
      as: 'return_items_return',
      foreignKey: {
          name: 'returnId',
      },
      constraints: false,
    });










//end loop



    db.returns.belongsTo(db.sales, {
      as: 'original_sale',
      foreignKey: {
        name: 'original_saleId',
      },
      constraints: false,
    });

    db.returns.belongsTo(db.stores, {
      as: 'store',
      foreignKey: {
        name: 'storeId',
      },
      constraints: false,
    });

    db.returns.belongsTo(db.users, {
      as: 'processed_by_user',
      foreignKey: {
        name: 'processed_by_userId',
      },
      constraints: false,
    });

    db.returns.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

    db.returns.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return returns;
};


