import { MigrationInterface, QueryRunner, getRepository } from 'typeorm'
import { Domain } from '@things-factory/shell'
import { Role, Privilege,User } from '@things-factory/auth-base'
const PRIVILEGES_SEED = {
  'modeller': [
    {
      name: 'mutation',
      category: 'setting'
    },
    {
      name: 'mutation',
      category: 'menu'
    },
    {
      name: 'mutation',
      category: 'user'
    },
    {
      name: 'mutation',
      category: 'appliance'
    },
    {
      name: 'mutation',
      category: 'appliance'
    },
    {
      name: 'mutation',
      category: 'application'
    },
    {
      name: 'mutation',
      category: 'board'
    },
    {
      name: 'mutation',
      category: 'group'
    },
    {
      name: 'mutation',
      category: 'play-group'
    },
    {
      name: 'mutation',
      category: 'fonts'
    },
    {
      name: 'mutation',
      category: 'attachment'
    },
    {
      name: 'mutation',
      category: 'scenario'
    },
    {
      name: 'mutation',
      category: 'step'
    },
    {
      name: 'query',
      category: 'setting'
    },
    {
      name: 'query',
      category: 'menu'
    },
    {
      name: 'query',
      category: 'user'
    },
    {
      name: 'query',
      category: 'appliance'
    },
    {
      name: 'query',
      category: 'application'
    },
    {
      name: 'query',
      category: 'board'
    },
    {
      name: 'query',
      category: 'group'
    },
    {
      name: 'query',
      category: 'play-group'
    },
    {
      name: 'query',
      category: 'fonts'
    },
    {
      name: 'query',
      category: 'attachment'
    },
    {
      name: 'query',
      category: 'scenario'
    },
    {
      name: 'query',
      category: 'step'
    }
  ],
  'system': [
    {
      name: 'mutation',
      category: 'system'
    },
    {
      name: 'query',
      category: 'system'
    }
  ],
  'viewer':[
    {
      name: 'query',
      category: 'board'
    },
    {
      name: 'query',
      category: 'group'
    },
    {
      name: 'query',
      category: 'play-group'
    }
  ]
}

const SEED = [
  {
    name: 'modeller',
    description: 'Board Modeller'
  },
  {
    name: 'viewer',
    description: 'Board Viewer'
  }
]

export class SeedRole1610516350460 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<any> {
    const repository = getRepository(Role)
    const userRepository= getRepository(User)

    let users:  User = await userRepository.find({ where: { userType:'admin' }, relations: ['domains'] })
    let domain: Domain = await getRepository(Domain).findOne({
      name: 'SYSTEM'
    })
    
    var privilegeSeeds = []
    Promise.all(
      SEED.map(async role => {
        var privileges= await this.getPrivileges(privilegeSeeds,role.name);
        await repository.save({
          ...role,
          'privileges':privileges,
          users,
          domain
        })
      })
    )
    await this.saveSystemRole(repository,users,domain);
  }

  public async saveSystemRole(repository,users,domain){
    const privilegeRepository= getRepository(Privilege)
    var role =   {
      name: 'system',
      description: 'System Admin'
    }
    var privileges = await privilegeRepository.find();
    await repository.save({
      ...role,
      'privileges':privileges,
      users,
      domain
    })
  }

  public async getPrivileges(privilegeSeeds,roleName){
    const privilegeRepository= getRepository(Privilege)
    privilegeSeeds = PRIVILEGES_SEED[roleName]
    var privileges = []
    await Promise.all(
      privilegeSeeds.map(async privilege => {
        var value = await privilegeRepository.findOne({ where: { name: privilege.name, category: privilege.category }})
        if(value){
          privileges.push(value)
        }
      })
    )
    return privileges
  }

  public async down(queryRunner: QueryRunner): Promise<any> {
    const repository = getRepository(Role)
    const privilegeRepository = getRepository(Privilege)
    const domain: Domain = await getRepository(Domain).findOne({
      name: 'SYSTEM'
    })

    await Promise.all(
      SEED.reverse().map(async role => {
        await repository.delete({ name: role.name, domain })
      })
    )
    
    await repository.delete({ name: 'system', domain })
  }
}
