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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

max_occupancy: {
        type: DataTypes.INTEGER,
      
      

      },

bedrooms: {
        type: DataTypes.INTEGER,
      
      

      },

bathrooms: {
        type: DataTypes.INTEGER,
      
      

      },

size_sqm: {
        type: DataTypes.DECIMAL,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

base_nightly_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

base_monthly_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

minimum_stay_nights: {
        type: DataTypes.INTEGER,
      
      

      },

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

  unit_types.associate = (db) => {

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

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


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












    db.unit_types.hasMany(db.units, {
      as: 'units_unit_type',
      foreignKey: {
          name: 'unit_typeId',
      },
      constraints: false,
    });



    db.unit_types.hasMany(db.negotiated_rates, {
      as: 'negotiated_rates_unit_type',
      foreignKey: {
          name: 'unit_typeId',
      },
      constraints: false,
    });


    db.unit_types.hasMany(db.booking_requests, {
      as: 'booking_requests_preferred_unit_type',
      foreignKey: {
          name: 'preferred_unit_typeId',
      },
      constraints: false,
    });




    db.unit_types.hasMany(db.reservations, {
      as: 'reservations_unit_type',
      foreignKey: {
          name: 'unit_typeId',
      },
      constraints: false,
    });















//end loop



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

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




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

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



  return unit_types;
};


