import { MigrationInterface, QueryRunner } from 'typeorm'
import { getRepository } from 'typeorm'
import { Domain } from '@things-factory/shell'
import { Board, PlayGroup } from '@things-factory/board-service'

const SEED_PLAY_GROUP = []

export class SeedPlayGroup1558009487900 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<any> {
    const repository = getRepository(PlayGroup)
    const domainRepository = getRepository(Domain)
    const domain = await domainRepository.findOne({ subdomain: 'system' })

    const boardRepository = getRepository(Board)
    const boards = await boardRepository.find()

    return await Promise.all(
      SEED_PLAY_GROUP.map(async group => {
        await repository.save({
          ...group,
          domain,
          boards
        })
      })
    )
  }

  public async down(queryRunner: QueryRunner): Promise<any> {
    const repository = getRepository(PlayGroup)

    return await Promise.all(
      SEED_PLAY_GROUP.reverse().map(async group => {
        let record = await repository.findOne({ name: group.name })

        // TODO remove cascade
        await repository.remove(record)
      })
    )
  }
}
