
package net.shyshkin.study.fullstack.supportportal.backend.config;

import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
import net.shyshkin.study.fullstack.supportportal.backend.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.UUID;

import static net.shyshkin.study.fullstack.supportportal.backend.constant.Authority.*;

@Configuration
public class DataInitializer {

    @Bean
    public CommandLineRunner init(UserRepository userRepository, PasswordEncoder passwordEncoder) {
        return args -> {
            System.out.println("Running DataInitializer...");

            // Initialize users
            createUserIfNotExists(userRepository, passwordEncoder, "admin", "adminpassword", "admin@example.com", "Admin", "User", "ROLE_ADMIN");
            createUserIfNotExists(userRepository, passwordEncoder, "hr", "hrpassword", "hr@example.com", "HR", "User", "ROLE_HR");
            createUserIfNotExists(userRepository, passwordEncoder, "manager", "managerpassword", "manager@example.com", "Manager", "User", "ROLE_MANAGER");
            createUserIfNotExists(userRepository, passwordEncoder, "user", "userpassword", "user@example.com", "Regular", "User", "ROLE_USER");
            createUserIfNotExists(userRepository, passwordEncoder, "superadmin", "superadminpassword", "superadmin@example.com", "Super", "Admin", "ROLE_SUPER_ADMIN");
        };
    }

    private void createUserIfNotExists(UserRepository userRepository, PasswordEncoder passwordEncoder,
                                       String username, String password, String email,
                                       String firstName, String lastName, String role) {
        if (userRepository.findByUsername(username).isEmpty()) {
            String encodedPassword = passwordEncoder.encode(password);

            User user = User.builder()
                    .email(email)
                    .firstName(firstName)
                    .lastName(lastName)
                    .username(username)
                    .password(encodedPassword)
                    .userId(UUID.randomUUID())
                    .isActive(true)
                    .isNotLocked(true)
                    .role(role)
                    .authorities(getAuthoritiesByRole(role))
                    .build();

            userRepository.save(user);
            System.out.println(role + " user created successfully.");
        } else {
            System.out.println(role + " user already exists.");
        }
    }

    private String[] getAuthoritiesByRole(String role) {
        switch (role) {
            case "ROLE_ADMIN":
                return ADMIN_AUTHORITIES;
            case "ROLE_HR":
                return HR_AUTHORITIES;
            case "ROLE_MANAGER":
                return MANAGER_AUTHORITIES;
            case "ROLE_USER":
                return USER_AUTHORITIES;
            case "ROLE_SUPER_ADMIN":
                return SUPER_ADMIN_AUTHORITIES;
            default:
                throw new IllegalArgumentException("Invalid role: " + role);
        }
    }
}
