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

po_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"partially_received",


"received",


"closed",


"cancelled"

        ],

      },

order_date: {
        type: DataTypes.DATE,
      
      

      },

expected_at: {
        type: DataTypes.DATE,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  purchase_orders.associate = (db) => {


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































    db.purchase_orders.hasMany(db.purchase_order_lines, {
      as: 'purchase_order_lines_purchase_order',
      foreignKey: {
          name: 'purchase_orderId',
      },
      constraints: false,
    });














//end loop



    db.purchase_orders.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

    db.purchase_orders.belongsTo(db.users, {
      as: 'buyer',
      foreignKey: {
        name: 'buyerId',
      },
      constraints: false,
    });



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


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

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



  return purchase_orders;
};


