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,
      
      

      },

short_name: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"ACTIVE",


"INACTIVE",


"SUSPENDED"

        ],

      },

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

legal_form: {
        type: DataTypes.ENUM,
      
      

        values: [

"EINZELUNTERNEHMEN",


"GBR",


"UG",


"GMBH",


"AG",


"KG",


"OHG",


"GMBH_CO_KG",


"OTHER"

        ],

      },

handelsregister_number: {
        type: DataTypes.TEXT,
      
      

      },

register_court: {
        type: DataTypes.TEXT,
      
      

      },

managing_directors: {
        type: DataTypes.TEXT,
      
      

      },

founding_date: {
        type: DataTypes.DATE,
      
      

      },

vat_number: {
        type: DataTypes.TEXT,
      
      

      },

tax_number: {
        type: DataTypes.TEXT,
      
      

      },

vat_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"VAT_REGISTERED",


"NOT_VAT_REGISTERED",


"SMALL_BUSINESS_19_USTG"

        ],

      },

eori_number: {
        type: DataTypes.TEXT,
      
      

      },

gln_number: {
        type: DataTypes.TEXT,
      
      

      },

scac: {
        type: DataTypes.TEXT,
      
      

      },

address_line1: {
        type: DataTypes.TEXT,
      
      

      },

address_line2: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

country_code: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

website: {
        type: DataTypes.TEXT,
      
      

      },

dispatch_email: {
        type: DataTypes.TEXT,
      
      

      },

accounting_email: {
        type: DataTypes.TEXT,
      
      

      },

hr_email: {
        type: DataTypes.TEXT,
      
      

      },

default_currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"EUR",


"USD",


"GBP",


"PLN",


"CZK",


"RON",


"BGN",


"OTHER"

        ],

      },

invoice_prefix: {
        type: DataTypes.TEXT,
      
      

      },

invoice_numbering_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"SEQUENTIAL",


"YEARLY_RESET",


"MONTHLY_RESET"

        ],

      },

invoice_next_number: {
        type: DataTypes.INTEGER,
      
      

      },

invoice_rounding: {
        type: DataTypes.ENUM,
      
      

        values: [

"NONE",


"ROUND_2_DECIMALS",


"ROUND_0_DECIMALS"

        ],

      },

payment_terms_days: {
        type: DataTypes.INTEGER,
      
      

      },

payroll_provider: {
        type: DataTypes.TEXT,
      
      

      },

payroll_contact_email: {
        type: DataTypes.TEXT,
      
      

      },

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


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


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


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


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


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



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






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


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



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


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


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


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


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


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


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


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


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


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



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


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


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


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


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



    db.companies.hasMany(db.pod_files, {
      as: 'pod_files_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.cost_items, {
      as: 'cost_items_company',
      foreignKey: {
          name: 'companyId',
      },
      constraints: false,
    });






//end loop





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


