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

name: {
        type: DataTypes.TEXT,
      
      

      },

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

  companies.associate = (db) => {


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


    db.companies.hasMany(db.users, {
      as: 'users_companies',
      foreignKey: {
          name: 'companiesId',
      },
      constraints: false,
    });





    db.companies.hasMany(db.business_units, {
      as: 'business_units_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.tags, {
      as: 'tags_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.assets, {
      as: 'assets_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.tenants, {
      as: 'tenants_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.leases, {
      as: 'leases_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.work_orders, {
      as: 'work_orders_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.products, {
      as: 'products_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.warehouses, {
      as: 'warehouses_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.inventory_items, {
      as: 'inventory_items_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.customers, {
      as: 'customers_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.sales_orders, {
      as: 'sales_orders_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.sales_order_lines, {
      as: 'sales_order_lines_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.invoices, {
      as: 'invoices_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.payments, {
      as: 'payments_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.crm_pipelines, {
      as: 'crm_pipelines_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.crm_stages, {
      as: 'crm_stages_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.leads, {
      as: 'leads_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.activities, {
      as: 'activities_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.content_folders, {
      as: 'content_folders_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.content_items, {
      as: 'content_items_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.seo_keywords, {
      as: 'seo_keywords_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.content_campaigns, {
      as: 'content_campaigns_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.dashboards, {
      as: 'dashboards_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });


    db.companies.hasMany(db.reports, {
      as: 'reports_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });



//end loop






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

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



  return companies;
};


