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

vehicle_code: {
        type: DataTypes.TEXT,
      
      

      },

plate_no: {
        type: DataTypes.TEXT,
      
      

      },

engine_no: {
        type: DataTypes.TEXT,
      
      

      },

chassis_no: {
        type: DataTypes.TEXT,
      
      

      },

make: {
        type: DataTypes.TEXT,
      
      

      },

model: {
        type: DataTypes.TEXT,
      
      

      },

year: {
        type: DataTypes.INTEGER,
      
      

      },

colour: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.TEXT,
      
      

      },

purchase_date: {
        type: DataTypes.DATE,
      
      

      },

purchase_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

ownership_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"owned",


"leased",


"financed",


"rented"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"available",


"assigned",


"maintenance",


"inactive",


"sold"

        ],

      },

branch: {
        type: DataTypes.TEXT,
      
      

      },

odometer: {
        type: DataTypes.INTEGER,
      
      

      },

insurance_policy_no: {
        type: DataTypes.TEXT,
      
      

      },

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

  vehicles.associate = (db) => {


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








    db.vehicles.hasMany(db.agreements, {
      as: 'agreements_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });



    db.vehicles.hasMany(db.invoices, {
      as: 'invoices_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });



    db.vehicles.hasMany(db.invoice_lines, {
      as: 'invoice_lines_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });





    db.vehicles.hasMany(db.vehicle_expenses, {
      as: 'vehicle_expenses_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });


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


    db.vehicles.hasMany(db.bills, {
      as: 'bills_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });




    db.vehicles.hasMany(db.ledger_entries, {
      as: 'ledger_entries_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });


    db.vehicles.hasMany(db.loan_schedules, {
      as: 'loan_schedules_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });


    db.vehicles.hasMany(db.vehicle_compliance, {
      as: 'vehicle_compliance_vehicle',
      foreignKey: {
          name: 'vehicleId',
      },
      constraints: false,
    });





//end loop



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

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




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

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



  return vehicles;
};


