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

name: {
        type: DataTypes.TEXT,
      
      

      },

building_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"market",


"harbor",


"road",


"tourist_attraction",


"house",


"bakery",


"school",


"church",


"abandoned"

        ],

      },

condition_state: {
        type: DataTypes.ENUM,
      
      

        values: [

"ruined",


"poor",


"fair",


"good",


"excellent"

        ],

      },

upgrade_level: {
        type: DataTypes.INTEGER,
      
      

      },

restored_at: {
        type: DataTypes.DATE,
      
      

      },

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

  buildings.associate = (db) => {


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














































    db.buildings.hasMany(db.construction_projects, {
      as: 'construction_projects_building',
      foreignKey: {
          name: 'buildingId',
      },
      constraints: false,
    });






//end loop



    db.buildings.belongsTo(db.locations, {
      as: 'location',
      foreignKey: {
        name: 'locationId',
      },
      constraints: false,
    });



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


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

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



  return buildings;
};


