import { SubCommunity } from '../../api/sub-communities/models'
import { Course } from '../../api/courses/models'
import { Chat } from '../../api/chats/models'

// MIT Community/College
const communityId = '6940fb887695b40bdcd6043f'
const collegeId = '6940fb887695b40bdcd6043f'

async function execute() {
  console.log('Adding sub communities from courses')
  await SubCommunity.deleteMany({community: communityId})

  const courses = await Course.find({college: collegeId}, {name: 1})
  const chats = await Chat.find({course: {$exists: true}})
  await SubCommunity.insertMany(
    courses.map(e => ({_id: e._id, community: communityId, name: e.name, isCourse: true}))
  )
  
  await Chat.updateMany({course: {$exists: true}}, {$set: {community: communityId}})

  for(let i = 0; i < chats.length; i++) {
    chats[i].subCommunity = chats[i].course
    // @ts-ignore
    chats[i].community = communityId
    chats[i].save().then(() => console.log(i , '/', chats.length))
  }
  
}

const scriptAddSubCommunitiesFromCourses = {
  execute
}

export default scriptAddSubCommunitiesFromCourses
