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,
      
      

      },

order_date: {
        type: DataTypes.DATE,
      
      

      },

expected_delivery_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"sent",


"confirmed",


"partially_received",


"received",


"cancelled",


"closed"

        ],

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"EUR",


"GBP",


"AED",


"SAR",


"INR"

        ],

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_total: {
        type: DataTypes.DECIMAL,
      
      

      },

grand_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,
    });


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



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














//end loop



    db.purchase_orders.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.purchase_orders.belongsTo(db.vendors, {
      as: 'vendor',
      foreignKey: {
        name: 'vendorId',
      },
      constraints: false,
    });

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

    db.purchase_orders.belongsTo(db.purchase_requisitions, {
      as: 'source_requisition',
      foreignKey: {
        name: 'source_requisitionId',
      },
      constraints: false,
    });

    db.purchase_orders.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      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;
};


