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

bill_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"pending_approval",


"approved",


"posted",


"partially_paid",


"paid",


"void"

        ],

      },

bill_date: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_total: {
        type: DataTypes.DECIMAL,
      
      

      },

total: {
        type: DataTypes.DECIMAL,
      
      

      },

balance_due: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  vendor_bills.associate = (db) => {

    db.vendor_bills.belongsToMany(db.vendor_bill_lines, {
      as: 'lines',
      foreignKey: {
        name: 'vendor_bills_linesId',
      },
      constraints: false,
      through: 'vendor_billsLinesVendor_bill_lines',
    });

    db.vendor_bills.belongsToMany(db.vendor_bill_lines, {
      as: 'lines_filter',
      foreignKey: {
        name: 'vendor_bills_linesId',
      },
      constraints: false,
      through: 'vendor_billsLinesVendor_bill_lines',
    });


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








































    db.vendor_bills.hasMany(db.vendor_bill_lines, {
      as: 'vendor_bill_lines_vendor_bill',
      foreignKey: {
          name: 'vendor_billId',
      },
      constraints: false,
    });






    db.vendor_bills.hasMany(db.payments, {
      as: 'payments_vendor_bill',
      foreignKey: {
          name: 'vendor_billId',
      },
      constraints: false,
    });












//end loop



    db.vendor_bills.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

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

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



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


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

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



  return vendor_bills;
};


