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

invoice_number: {
        type: DataTypes.TEXT,
      
      

      },

invoice_date: {
        type: DataTypes.DATE,
      
      

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected",


"paid",


"void"

        ],

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"EUR",


"GBP",


"AED",


"SAR",


"INR"

        ],

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  vendor_invoices.associate = (db) => {


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




















    db.vendor_invoices.hasMany(db.invoice_lines, {
      as: 'invoice_lines_vendor_invoice',
      foreignKey: {
          name: 'vendor_invoiceId',
      },
      constraints: false,
    });


    db.vendor_invoices.hasMany(db.payments, {
      as: 'payments_vendor_invoice',
      foreignKey: {
          name: 'vendor_invoiceId',
      },
      constraints: false,
    });












//end loop



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

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

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

    db.vendor_invoices.belongsTo(db.goods_receipts, {
      as: 'goods_receipt',
      foreignKey: {
        name: 'goods_receiptId',
      },
      constraints: false,
    });

    db.vendor_invoices.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return vendor_invoices;
};


