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 organizations = sequelize.define(
    'organizations',
    {
      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,
    },
  );

  organizations.associate = (db) => {


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


    db.organizations.hasMany(db.users, {
      as: 'users_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });





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


    db.organizations.hasMany(db.user_role_assignments, {
      as: 'user_role_assignments_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.role_permission_mappings, {
      as: 'role_permission_mappings_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.vendors, {
      as: 'vendors_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.warehouses, {
      as: 'warehouses_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.product_categories, {
      as: 'product_categories_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.products, {
      as: 'products_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.purchase_requisitions, {
      as: 'purchase_requisitions_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.purchase_requisition_lines, {
      as: 'purchase_requisition_lines_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.purchase_orders, {
      as: 'purchase_orders_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.purchase_order_lines, {
      as: 'purchase_order_lines_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.goods_receipts, {
      as: 'goods_receipts_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.goods_receipt_lines, {
      as: 'goods_receipt_lines_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.vendor_invoices, {
      as: 'vendor_invoices_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.invoice_lines, {
      as: 'invoice_lines_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.payments, {
      as: 'payments_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.departments, {
      as: 'departments_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.document_types, {
      as: 'document_types_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.documents, {
      as: 'documents_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.document_links, {
      as: 'document_links_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.approval_workflows, {
      as: 'approval_workflows_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.approval_steps, {
      as: 'approval_steps_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.approvals, {
      as: 'approvals_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.activity_logs, {
      as: 'activity_logs_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.ui_announcements, {
      as: 'ui_announcements_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });



//end loop






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

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



  return organizations;
};


