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

maintenance_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"service",


"repair",


"inspection",


"tire_change",


"oil_change",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"scheduled",


"in_progress",


"completed",


"cancelled"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

odometer_km: {
        type: DataTypes.INTEGER,
      
      

      },

cost_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  maintenance_records.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.maintenance_records.belongsTo(db.vehicles, {
      as: 'vehicle',
      foreignKey: {
        name: 'vehicleId',
      },
      constraints: false,
    });

    db.maintenance_records.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return maintenance_records;
};


