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,
      
      

      },

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

      },

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

      },

      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.currency_rates, {
      as: 'currency_rates_from_currency',
      foreignKey: {
          name: 'from_currencyId',
      },
      constraints: false,
    });

    db.currencies.hasMany(db.currency_rates, {
      as: 'currency_rates_to_currency',
      foreignKey: {
          name: 'to_currencyId',
      },
      constraints: false,
    });











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


    db.currencies.hasMany(db.cash_boxes, {
      as: 'cash_boxes_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.letters_of_credit, {
      as: 'letters_of_credit_currency',
      foreignKey: {
          name: 'currencyId',
      },
      constraints: false,
    });










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














//end loop



    db.currencies.belongsTo(db.businesses, {
      as: 'business',
      foreignKey: {
        name: 'businessId',
      },
      constraints: false,
    });




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

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



  return currencies;
};


