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 drivers = sequelize.define(
    'drivers',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

driver_code: {
        type: DataTypes.TEXT,
      
      

      },

driver_name: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

license_number: {
        type: DataTypes.TEXT,
      
      

      },

license_expiry_date: {
        type: DataTypes.DATE,
      
      

      },

national_id: {
        type: DataTypes.TEXT,
      
      

      },

trn_tax_id: {
        type: DataTypes.TEXT,
      
      

      },

security_deposit: {
        type: DataTypes.DECIMAL,
      
      

      },

credit_limit: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive",


"suspended",


"blacklisted"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

modified_on: {
        type: DataTypes.DATE,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  drivers.associate = (db) => {


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







    db.drivers.hasMany(db.vehicles, {
      as: 'vehicles_current_driver',
      foreignKey: {
          name: 'current_driverId',
      },
      constraints: false,
    });


    db.drivers.hasMany(db.agreements, {
      as: 'agreements_driver',
      foreignKey: {
          name: 'driverId',
      },
      constraints: false,
    });



    db.drivers.hasMany(db.invoices, {
      as: 'invoices_driver',
      foreignKey: {
          name: 'driverId',
      },
      constraints: false,
    });




    db.drivers.hasMany(db.payments, {
      as: 'payments_driver',
      foreignKey: {
          name: 'driverId',
      },
      constraints: false,
    });





    db.drivers.hasMany(db.driver_expenses, {
      as: 'driver_expenses_driver',
      foreignKey: {
          name: 'driverId',
      },
      constraints: false,
    });





    db.drivers.hasMany(db.ledger_entries, {
      as: 'ledger_entries_driver',
      foreignKey: {
          name: 'driverId',
      },
      constraints: false,
    });







//end loop



    db.drivers.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });




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

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



  return drivers;
};


