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

date: {
        type: DataTypes.DATE,
      
      

      },

expense_category: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

charge_to_driver_flag: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

billable_to_driver_flag: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"approved",


"posted",


"voided",


"reversed"

        ],

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

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

  driver_expenses.associate = (db) => {


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



























//end loop



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

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

    db.driver_expenses.belongsTo(db.vehicles, {
      as: 'vehicle',
      foreignKey: {
        name: 'vehicleId',
      },
      constraints: false,
    });

    db.driver_expenses.belongsTo(db.agreements, {
      as: 'agreement',
      foreignKey: {
        name: 'agreementId',
      },
      constraints: false,
    });

    db.driver_expenses.belongsTo(db.invoices, {
      as: 'invoice',
      foreignKey: {
        name: 'invoiceId',
      },
      constraints: false,
    });

    db.driver_expenses.belongsTo(db.chart_of_accounts, {
      as: 'expense_account',
      foreignKey: {
        name: 'expense_accountId',
      },
      constraints: false,
    });

    db.driver_expenses.belongsTo(db.chart_of_accounts, {
      as: 'payment_account',
      foreignKey: {
        name: 'payment_accountId',
      },
      constraints: false,
    });



    db.driver_expenses.hasMany(db.file, {
      as: 'attachment',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.driver_expenses.getTableName(),
        belongsToColumn: 'attachment',
      },
    });


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

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



  return driver_expenses;
};


