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

name: {
        type: DataTypes.TEXT,
      
      

      },

rate: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"vat",


"gst",


"sales_tax",


"withholding",


"none"

        ],

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  tax_rates.associate = (db) => {


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










    db.tax_rates.hasMany(db.contacts, {
      as: 'contacts_default_tax_rate',
      foreignKey: {
          name: 'default_tax_rateId',
      },
      constraints: false,
    });


    db.tax_rates.hasMany(db.products, {
      as: 'products_tax_rate',
      foreignKey: {
          name: 'tax_rateId',
      },
      constraints: false,
    });





    db.tax_rates.hasMany(db.quote_lines, {
      as: 'quote_lines_tax_rate',
      foreignKey: {
          name: 'tax_rateId',
      },
      constraints: false,
    });



    db.tax_rates.hasMany(db.invoice_lines, {
      as: 'invoice_lines_tax_rate',
      foreignKey: {
          name: 'tax_rateId',
      },
      constraints: false,
    });



    db.tax_rates.hasMany(db.bill_lines, {
      as: 'bill_lines_tax_rate',
      foreignKey: {
          name: 'tax_rateId',
      },
      constraints: false,
    });


    db.tax_rates.hasMany(db.expenses, {
      as: 'expenses_tax_rate',
      foreignKey: {
          name: 'tax_rateId',
      },
      constraints: false,
    });










//end loop



    db.tax_rates.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return tax_rates;
};


