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

receipt_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"posted",


"cancelled"

        ],

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

carrier_reference: {
        type: DataTypes.TEXT,
      
      

      },

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

  receipts.associate = (db) => {


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























    db.receipts.hasMany(db.receipt_lines, {
      as: 'receipt_lines_receipt',
      foreignKey: {
          name: 'receiptId',
      },
      constraints: false,
    });























//end loop



    db.receipts.belongsTo(db.purchase_orders, {
      as: 'purchase_order',
      foreignKey: {
        name: 'purchase_orderId',
      },
      constraints: false,
    });

    db.receipts.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

    db.receipts.belongsTo(db.users, {
      as: 'received_by',
      foreignKey: {
        name: 'received_byId',
      },
      constraints: false,
    });



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


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

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



  return receipts;
};


