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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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



//end loop






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

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



  return companies;
};


