<%_ const UserEntity = user.persistClass %>
import { MigrationInterface, QueryRunner } from 'typeorm';
<%_ if (authenticationType !== 'oauth2') { _%>
import { <%= UserEntity %> } from '../domain/user.entity';
import { transformPassword } from '../security';
<%_ } _%>
import { Authority } from '../domain/authority.entity';

export class SeedUsersRoles1570200490072 implements MigrationInterface {
  role1: Authority = { name: 'ROLE_ADMIN' };

  role2: Authority = { name: 'ROLE_USER' };

  <%_ if (authenticationType !== 'oauth2') { _%>
  user1: <%= UserEntity %> = {
    login: 'system',
    password: 'system',
    firstName: 'System',
    lastName: 'System',
    email: 'system@localhost.it',
    imageUrl: '',
    activated: true,
    langKey: 'en',
    createdBy: 'system',
    lastModifiedBy: 'system'
  };

  user2: <%= UserEntity %> = {
    login: 'anonymoususer',
    password: 'anonymoususer',
    firstName: 'Anonymous',
    lastName: 'User',
    email: 'anonymoususer@localhost.it',
    imageUrl: '',
    activated: true,
    langKey: 'en',
    createdBy: 'system',
    lastModifiedBy: 'system'
  };

  user3: <%= UserEntity %> = {
    login: 'admin',
    password: 'admin',
    firstName: 'Administrator',
    lastName: 'Administrator',
    email: 'admin@localhost.it',
    imageUrl: '',
    activated: true,
    langKey: 'en',
    createdBy: 'system',
    lastModifiedBy: 'system'
  };

  user4: <%= UserEntity %> = {
    login: 'user',
    password: 'user',
    firstName: 'User',
    lastName: 'User',
    email: 'user@localhost.it',
    imageUrl: '',
    activated: true,
    langKey: 'en',
    createdBy: 'system',
    lastModifiedBy: 'system'
  };

  <%_ } _%>
  // eslint-disable-next-line
  public async up(queryRunner: QueryRunner): Promise<any> {

    const authorityRepository = queryRunner.connection.getRepository("<%- jhiTablePrefix %>_authority");

   <%_ if (authenticationType === 'oauth2') { _%>

    await authorityRepository.save([this.role1, this.role2]);

   <%_ } else { _%>

    const adminRole = await authorityRepository.save(this.role1);
    const userRole = await authorityRepository.save(this.role2);

    const userRepository = queryRunner.connection.getRepository("<%- jhiTablePrefix %>_user");

    this.user1.authorities= [adminRole, userRole];
    this.user3.authorities= [adminRole, userRole];
    this.user4.authorities= [userRole];

    const users = await Promise.all([this.user1, this.user2, this.user3, this.user4].map(u => transformPassword({ ...u })));

    await userRepository.save(users);
    <%_ } _%>
  }


  // eslint-disable-next-line
  public async down(queryRunner: QueryRunner): Promise<any> { }
}
