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,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

receipt_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"received",


"put_away",


"closed",


"cancelled"

        ],

      },

supplier_document_ref: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        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.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

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

    db.receipts.belongsTo(db.locations, {
      as: 'to_location',
      foreignKey: {
        name: 'to_locationId',
      },
      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;
};


