module.exports = function(sequelize, DataTypes) {
  const place_offerings = sequelize.define(
    'place_offerings',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      name: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      description: {
        type: DataTypes.TEXT,
      },
      offering_type: {
        type: DataTypes.ENUM,
        values: ['product', 'service'],
        allowNull: false,
        defaultValue: 'product',
      },
      price: {
        type: DataTypes.DECIMAL,
      },
      stock_status: {
        type: DataTypes.ENUM,
        values: ['in_stock', 'limited', 'out_of_stock', 'by_request'],
        allowNull: false,
        defaultValue: 'by_request',
      },
      stock_quantity: {
        type: DataTypes.INTEGER,
      },
      is_verified: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      is_active: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true,
      },
      last_stock_update: {
        type: DataTypes.DATE,
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  place_offerings.associate = (db) => {
    db.place_offerings.belongsTo(db.places, {
      as: 'place',
      foreignKey: {
        name: 'placeId',
      },
      constraints: false,
    });

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

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

  return place_offerings;
};
