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

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"FUEL",


"TOLL",


"PARKING",


"WASH",


"MAINTENANCE",


"TIRES",


"DRIVER_EXPENSE",


"PER_DIEM",


"FERRY",


"TRAIN",


"FINES",


"OTHER"

        ],

      },

occurred_at: {
        type: DataTypes.DATE,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"EUR",


"USD",


"GBP",


"PLN",


"CZK",


"RON",


"BGN",


"OTHER"

        ],

      },

vendor: {
        type: DataTypes.TEXT,
      
      

      },

reference: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  cost_items.associate = (db) => {


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








































    db.cost_items.hasMany(db.invoice_lines, {
      as: 'invoice_lines_cost_item',
      foreignKey: {
          name: 'cost_itemId',
      },
      constraints: false,
    });




    db.cost_items.hasMany(db.cost_files, {
      as: 'cost_files_cost_item',
      foreignKey: {
          name: 'cost_itemId',
      },
      constraints: false,
    });


    db.cost_items.hasMany(db.fuel_entries, {
      as: 'fuel_entries_cost_item',
      foreignKey: {
          name: 'cost_itemId',
      },
      constraints: false,
    });


    db.cost_items.hasMany(db.toll_entries, {
      as: 'toll_entries_cost_item',
      foreignKey: {
          name: 'cost_itemId',
      },
      constraints: false,
    });



//end loop



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

    db.cost_items.belongsTo(db.trips, {
      as: 'trip',
      foreignKey: {
        name: 'tripId',
      },
      constraints: false,
    });

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

    db.cost_items.belongsTo(db.trailers, {
      as: 'trailer',
      foreignKey: {
        name: 'trailerId',
      },
      constraints: false,
    });

    db.cost_items.belongsTo(db.employees, {
      as: 'employee',
      foreignKey: {
        name: 'employeeId',
      },
      constraints: false,
    });

    db.cost_items.belongsTo(db.loads, {
      as: 'load',
      foreignKey: {
        name: 'loadId',
      },
      constraints: false,
    });

    db.cost_items.belongsTo(db.vendors, {
      as: 'vendor_rel',
      foreignKey: {
        name: 'vendor_relId',
      },
      constraints: false,
    });

    db.cost_items.belongsTo(db.fuel_entries, {
      as: 'fuel_entry',
      foreignKey: {
        name: 'fuel_entryId',
      },
      constraints: false,
    });

    db.cost_items.belongsTo(db.toll_entries, {
      as: 'toll_entry',
      foreignKey: {
        name: 'toll_entryId',
      },
      constraints: false,
    });



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


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

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



  return cost_items;
};


