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",


"sent",


"partially_received",


"received",


"cancelled",


"closed"

        ],

      },

order_date: {
        type: DataTypes.DATE,
      
      

      },

expected_delivery_at: {
        type: DataTypes.DATE,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

shipping_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

ship_to_address: {
        type: DataTypes.TEXT,
      
      

      },

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_line_items, {
      as: 'purchase_order_line_items_purchase_order',
      foreignKey: {
          name: 'purchase_orderId',
      },
      constraints: false,
    });


    db.purchase_orders.hasMany(db.goods_receipts, {
      as: 'goods_receipts_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.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

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




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

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



  return purchase_orders;
};


