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

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

symbol: {
        type: DataTypes.TEXT,
      
      

      },

minor_unit: {
        type: DataTypes.INTEGER,
      
      

      },

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

  currencies.associate = (db) => {


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










    db.currencies.hasMany(db.contacts, {
      as: 'contacts_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });





    db.currencies.hasMany(db.quotes, {
      as: 'quotes_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });



    db.currencies.hasMany(db.invoices, {
      as: 'invoices_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });



    db.currencies.hasMany(db.bills, {
      as: 'bills_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });



    db.currencies.hasMany(db.expenses, {
      as: 'expenses_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });


    db.currencies.hasMany(db.bank_accounts, {
      as: 'bank_accounts_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });


    db.currencies.hasMany(db.bank_transactions, {
      as: 'bank_transactions_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });


    db.currencies.hasMany(db.payments, {
      as: 'payments_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });







//end loop



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




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

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



  return currencies;
};


