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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

rate: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"vat",


"withholding",


"excise",


"none"

        ],

      },

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

      },

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

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  tax_codes.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_codes.hasMany(db.products, {
      as: 'products_tax_code',
      foreignKey: {
          name: 'tax_codeId',
      },
      constraints: false,
    });





    db.tax_codes.hasMany(db.quote_lines, {
      as: 'quote_lines_tax_code',
      foreignKey: {
          name: 'tax_codeId',
      },
      constraints: false,
    });



    db.tax_codes.hasMany(db.invoice_lines, {
      as: 'invoice_lines_tax_code',
      foreignKey: {
          name: 'tax_codeId',
      },
      constraints: false,
    });




    db.tax_codes.hasMany(db.expenses, {
      as: 'expenses_tax_code',
      foreignKey: {
          name: 'tax_codeId',
      },
      constraints: false,
    });










//end loop



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




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

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



  return tax_codes;
};


