import { Course } from '../../api/courses/models'
import { Chat } from '../../api/chats/models'
import { User } from '../../api/users/models'

const collegeId = '6940fb887695b40bdcd6043f'
const creatorId = '696f5b68f348ec4459569b58'

async function execute() {
  console.log('Adding courses chats')
  const creator = await User.findById(creatorId)
  if(!creator) {
    throw new Error('Creator not found')
  }
  const courses = await Course.find({college: collegeId}, {name: 1})
  const chats = courses.map(e => ({
    creator: creator._id,
    isGroup: true,
    category: 'Courses',
    name: e.name,
    course: e._id,
    participants: [{user: creatorId, isAdmin: true}]
  }))
  const result = await Chat.insertMany(chats)
  console.log('done!', result.length, 'chats created')
}

const scriptAddCoursesChats = {
  execute
}

export default scriptAddCoursesChats
