package info.rolandkrueger.userservice.application;

import info.rolandkrueger.userservice.model.Authority;
import info.rolandkrueger.userservice.model.User;
import info.rolandkrueger.userservice.repository.AuthorityRepository;
import info.rolandkrueger.userservice.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author Roland Krüger
 */
@Component
@Profile("DEV")
public class DevelopmentProfileConfiguration implements ApplicationListener<ContextRefreshedEvent> {

    private final static Logger LOG = LoggerFactory.getLogger(DevelopmentProfileConfiguration.class);

    public final static Authority admins, users, developers;
    public final static User alice, bob, charly;

    static {
        LOG.info("Creating test data: authorities 'admin', 'user', 'developer'");

        admins = new Authority("admin", "The admin role");
        users = new Authority("user", "The user role");
        developers = new Authority("developer", "The developer role");

        LOG.info("Creating test data: users 'alice', 'bob', 'charly'");
        alice = new User("alice");
        alice.setUnencryptedPassword("alice");
        alice.createRegistrationConfirmationToken();
        alice.setEmail("alice@example.com");
        alice.addAuthority(admins);

        bob = new User("bob");
        bob.setUnencryptedPassword("bob");
        bob.addAuthority(developers);
        bob.addAuthority(users);

        charly = new User("charly");
        charly.setLastLogin(LocalDateTime.now());
        charly.setUnencryptedPassword("charly");
    }

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AuthorityRepository authorityRepository;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        saveAuthorities();
        saveUsers();
    }

    private void saveAuthorities() {
        authorityRepository.save(Arrays.asList(admins, users, developers));
    }

    private void saveUsers() {
        final List<User> users = Arrays.asList(alice, bob, charly);
        userRepository.save(users);
        LOG.info("Added users {}", users);
    }
}

@@ -1012,32 +1012,48 @@
 hority admin
-s, users
+Authority, userAuthority
 , developers
@@ -1051,17 +1051,25 @@
 eveloper
-s
+Authority
 ;%0A    pu
@@ -1221,17 +1221,25 @@
    admin
-s
+Authority
  = new A
@@ -1287,17 +1287,25 @@
     user
-s
+Authority
  = new A
@@ -1356,17 +1356,25 @@
 eveloper
-s
+Authority
  = new A
@@ -1703,17 +1703,25 @@
 ty(admin
-s
+Authority
 );%0A%0A    
@@ -1824,17 +1824,25 @@
 eveloper
-s
+Authority
 );%0A     
@@ -1865,17 +1865,25 @@
 ity(user
-s
+Authority
 );%0A%0A    
@@ -2377,16 +2377,32 @@
 dmin
-s, users
+Authority, userAuthority
 , de
@@ -2408,17 +2408,25 @@
 eveloper
-s
+Authority
 ));%0A    

Rename Authority fields