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

return_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"pending_approval",


"approved",


"received",


"refunded",


"cancelled"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

refund_total: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  customer_returns.associate = (db) => {

    db.customer_returns.belongsToMany(db.customer_return_lines, {
      as: 'lines',
      foreignKey: {
        name: 'customer_returns_linesId',
      },
      constraints: false,
      through: 'customer_returnsLinesCustomer_return_lines',
    });

    db.customer_returns.belongsToMany(db.customer_return_lines, {
      as: 'lines_filter',
      foreignKey: {
        name: 'customer_returns_linesId',
      },
      constraints: false,
      through: 'customer_returnsLinesCustomer_return_lines',
    });


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


































    db.customer_returns.hasMany(db.customer_return_lines, {
      as: 'customer_return_lines_customer_return',
      foreignKey: {
          name: 'customer_returnId',
      },
      constraints: false,
    });























//end loop



    db.customer_returns.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

    db.customer_returns.belongsTo(db.invoices, {
      as: 'invoice',
      foreignKey: {
        name: 'invoiceId',
      },
      constraints: false,
    });

    db.customer_returns.belongsTo(db.sales_orders, {
      as: 'sales_order',
      foreignKey: {
        name: 'sales_orderId',
      },
      constraints: false,
    });

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




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

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



  return customer_returns;
};


