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

name: {
        type: DataTypes.TEXT,
      
      

      },

asset_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"land",


"building",


"unit",


"vehicle",


"equipment",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"available",


"rented",


"maintenance",


"inactive"

        ],

      },

asset_code: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

purchase_price: {
        type: DataTypes.DECIMAL,
      
      

      },

purchase_date: {
        type: DataTypes.DATE,
      
      

      },

current_valuation: {
        type: DataTypes.DECIMAL,
      
      

      },

valuation_date: {
        type: DataTypes.DATE,
      
      

      },

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

  assets.associate = (db) => {

    db.assets.belongsToMany(db.tags, {
      as: 'tags',
      foreignKey: {
        name: 'assets_tagsId',
      },
      constraints: false,
      through: 'assetsTagsTags',
    });

    db.assets.belongsToMany(db.tags, {
      as: 'tags_filter',
      foreignKey: {
        name: 'assets_tagsId',
      },
      constraints: false,
      through: 'assetsTagsTags',
    });


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










    db.assets.hasMany(db.leases, {
      as: 'leases_asset',
      foreignKey: {
          name: 'assetId',
      },
      constraints: false,
    });


    db.assets.hasMany(db.work_orders, {
      as: 'work_orders_asset',
      foreignKey: {
          name: 'assetId',
      },
      constraints: false,
    });






















//end loop



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

    db.assets.belongsTo(db.business_units, {
      as: 'business_unit',
      foreignKey: {
        name: 'business_unitId',
      },
      constraints: false,
    });



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

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


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

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



  return assets;
};


