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

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

trade_name: {
        type: DataTypes.TEXT,
      
      

      },

national_id: {
        type: DataTypes.TEXT,
      
      

      },

economic_code: {
        type: DataTypes.TEXT,
      
      

      },

registration_number: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  businesses.associate = (db) => {


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






    db.businesses.hasMany(db.branches, {
      as: 'branches_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.fiscal_years, {
      as: 'fiscal_years_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.user_role_assignments, {
      as: 'user_role_assignments_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });



    db.businesses.hasMany(db.role_permissions, {
      as: 'role_permissions_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.app_settings, {
      as: 'app_settings_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


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


    db.businesses.hasMany(db.currency_rates, {
      as: 'currency_rates_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.numbering_profiles, {
      as: 'numbering_profiles_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.widgets, {
      as: 'widgets_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });






    db.businesses.hasMany(db.counterparties, {
      as: 'counterparties_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.cost_centers, {
      as: 'cost_centers_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.projects, {
      as: 'projects_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.contracts, {
      as: 'contracts_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.cash_boxes, {
      as: 'cash_boxes_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.banks, {
      as: 'banks_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.bank_accounts, {
      as: 'bank_accounts_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.letters_of_credit, {
      as: 'letters_of_credit_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.pos_terminals, {
      as: 'pos_terminals_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.sales_item_groups, {
      as: 'sales_item_groups_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.warehouse_item_groups, {
      as: 'warehouse_item_groups_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.customer_groups, {
      as: 'customer_groups_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.analytic_dimensions, {
      as: 'analytic_dimensions_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.accounts, {
      as: 'accounts_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.journal_voucher_types, {
      as: 'journal_voucher_types_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.journal_vouchers, {
      as: 'journal_vouchers_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });



    db.businesses.hasMany(db.approval_workflows, {
      as: 'approval_workflows_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });




    db.businesses.hasMany(db.year_end_operations, {
      as: 'year_end_operations_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.report_definitions, {
      as: 'report_definitions_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });



    db.businesses.hasMany(db.chat_conversations, {
      as: 'chat_conversations_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });




    db.businesses.hasMany(db.form_alarms, {
      as: 'form_alarms_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });


    db.businesses.hasMany(db.data_export_jobs, {
      as: 'data_export_jobs_business',
      foreignKey: {
          name: 'businessId',
      },
      constraints: false,
    });



//end loop






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

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



  return businesses;
};


