
module.exports = function(sequelize, DataTypes) {
  const properties = sequelize.define(
    'properties',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  properties.associate = (db) => {

    db.properties.hasMany(db.unit_types, {
      as: 'unit_types',
      foreignKey: {
        name: 'propertyId',
      },
      constraints: false,
    });

    db.properties.hasMany(db.unit_types, {
      as: 'unit_types_filter',
      foreignKey: {
        name: 'propertyId',
      },
      constraints: false,
    });

    db.properties.hasMany(db.units, {
      as: 'units',
      foreignKey: {
        name: 'propertyId',
      },
      constraints: false,
    });

    db.properties.hasMany(db.units, {
      as: 'units_filter',
      foreignKey: {
        name: 'propertyId',
      },
      constraints: false,
    });

    db.properties.hasMany(db.amenities, {
      as: 'amenities',
      foreignKey: {
        name: 'propertyId',
      },
      constraints: false,
    });

    db.properties.hasMany(db.amenities, {
      as: 'amenities_filter',
      foreignKey: {
        name: 'propertyId',
      },
      constraints: false,
    });


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










    db.properties.hasMany(db.amenities, {
      as: 'amenities_property',
      foreignKey: {
          name: 'propertyId',
      },
      constraints: false,
    });


    db.properties.hasMany(db.unit_types, {
      as: 'unit_types_property',
      foreignKey: {
          name: 'propertyId',
      },
      constraints: false,
    });


    db.properties.hasMany(db.units, {
      as: 'units_property',
      foreignKey: {
          name: 'propertyId',
      },
      constraints: false,
    });



    db.properties.hasMany(db.negotiated_rates, {
      as: 'negotiated_rates_property',
      foreignKey: {
          name: 'propertyId',
      },
      constraints: false,
    });


    db.properties.hasMany(db.booking_requests, {
      as: 'booking_requests_preferred_property',
      foreignKey: {
          name: 'preferred_propertyId',
      },
      constraints: false,
    });




    db.properties.hasMany(db.reservations, {
      as: 'reservations_property',
      foreignKey: {
          name: 'propertyId',
      },
      constraints: false,
    });















//end loop



    db.properties.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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



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


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

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



  return properties;
};


