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,
      },

company_name: {
        type: DataTypes.TEXT,
      
      

      },

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

tax_number: {
        type: DataTypes.TEXT,
      
      

      },

website: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

address_line1: {
        type: DataTypes.TEXT,
      
      

      },

address_line2: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        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,
    },
  );

  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.user_role_assignments, {
      as: 'user_role_assignments_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });



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


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


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


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


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



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



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



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



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


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



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



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





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



//end loop



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



    db.companies.hasMany(db.file, {
      as: 'logo',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.companies.getTableName(),
        belongsToColumn: 'logo',
      },
    });


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

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



  return companies;
};


