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

unit_number: {
        type: DataTypes.TEXT,
      
      

      },

floor: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"available",


"occupied",


"maintenance",


"cleaning_hold",


"reserved",


"out_of_service"

        ],

      },

max_occupancy_override: {
        type: DataTypes.INTEGER,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  units.associate = (db) => {

    db.units.hasMany(db.unit_availability_blocks, {
      as: 'availability_blocks',
      foreignKey: {
        name: 'unitId',
      },
      constraints: false,
    });

    db.units.hasMany(db.unit_availability_blocks, {
      as: 'availability_blocks_filter',
      foreignKey: {
        name: 'unitId',
      },
      constraints: false,
    });


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













    db.units.hasMany(db.unit_availability_blocks, {
      as: 'unit_availability_blocks_unit',
      foreignKey: {
          name: 'unitId',
      },
      constraints: false,
    });






    db.units.hasMany(db.reservations, {
      as: 'reservations_unit',
      foreignKey: {
          name: 'unitId',
      },
      constraints: false,
    });















//end loop



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

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

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




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

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



  return units;
};


