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.customers, {
      as: 'customers_companies',
      foreignKey: {
        name: 'companiesId',
      },
      constraints: false,
    });

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    //end loop

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

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

  return companies;
};
