text: const mongoose = require('mongoose');
  
  const CourseSchema = new mongoose.Schema({
    title: {
      type: String,
      trim: true,
      required: [true, 'Please add a course title.'],
    },
    description: {
      type: String,
      required: [true, 'Please add a description.'],
    },
    weeks: {
      type: String,
      required: [true, 'Please add number of weeks.'],
    },
    tuition: {
      type: Number,
      required: [true, 'Please add a tuition cost.'],
    },
    minimumSkill: {
      type: String,
      required: [true, 'please add a minimum skill.'],
      enum: ['beginner', 'intermediate', 'advanced'],
    },
    scholarhipsAvailable: {
      type: Boolean,
      default: false,
    },
    createdAt: {
      type: Date,
      default: Date.now,
    },
    bootcamp: {
      type: mongoose.Schema.ObjectId,
      ref: 'Bootcamp',
      required: true,
    },
    user: {
      type: mongoose.Schema.ObjectId,
      ref: 'User',
      required: true,
    },
  });
  
  // Static method to get avg of course tuitions
  CourseSchema.statics.getAverageCost = async function (bootcampId) {
    const arrObj = await this.aggregate([
      {
        $match: { bootcamp: bootcampId },
      },
      {
        $group: { _id: '$bootcamp', averageCost: { $avg: '$tuition' } },
      },
    ]);
  
    try {
      await this.model('Bootcamp').findByIdAndUpdate(bootcampId, {
        averageCost: Math.ceil(arrObj[0].averageCost / 10) * 10,
      });
    } catch (error) {
      console.error(error);
    }
  };
  
  // Call getAverageCost after save
  CourseSchema.post('save', async function () {
    await this.constructor.getAverageCost(this.bootcamp);
  });
  
  // Call getAverageCost before remove
  CourseSchema.post('remove', async function () {
    await this.constructor.getAverageCost(this.bootcamp);
  });
  
  module.exports = mongoose.model('Course', CourseSchema);
  