package de.assecutor.votianlt.config;

import de.assecutor.votianlt.model.User;
import de.assecutor.votianlt.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Set;

@Component
public class DataInitializer implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(DataInitializer.class);

    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    public DataInitializer(UserRepository userRepository, PasswordEncoder passwordEncoder) {
        this.userRepository = userRepository;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public void run(String... args) throws Exception {
        initializeTestUsers();
    }

    private void initializeTestUsers() {
        log.info("Initializing test users...");

        // Admin User
        if (!userRepository.existsByEmail("admin@votianlt.de")) {
            User adminUser = new User();
            adminUser.setEmail("admin@votianlt.de");
            adminUser.setPassword(passwordEncoder.encode("admin123"));
            adminUser.setName("Administrator");
            adminUser.setFirstname("Admin");
            adminUser.setIsActivated((byte) 1);
            adminUser.setIsEmailConfirmed((byte) 1);
            adminUser.setCreatedAt(LocalDateTime.now());
            adminUser.setUpdatedAt(LocalDateTime.now());
            adminUser.setRoles(Set.of("USER", "ADMIN"));

            userRepository.save(adminUser);
            log.info("Created admin user: admin@votianlt.de / admin123");
        }

        // Test User
        if (!userRepository.existsByEmail("test@votianlt.de")) {
            User testUser = new User();
            testUser.setEmail("test@votianlt.de");
            testUser.setPassword(passwordEncoder.encode("test123"));
            testUser.setName("Testmann");
            testUser.setFirstname("Test");
            testUser.setIsActivated((byte) 1);
            testUser.setIsEmailConfirmed((byte) 1);
            testUser.setCreatedAt(LocalDateTime.now());
            testUser.setUpdatedAt(LocalDateTime.now());
            testUser.setRoles(Set.of("USER"));

            userRepository.save(testUser);
            log.info("Created test user: test@votianlt.de / test123");
        }

        // Demo User
        if (!userRepository.existsByEmail("demo@votianlt.de")) {
            User demoUser = new User();
            demoUser.setEmail("demo@votianlt.de");
            demoUser.setPassword(passwordEncoder.encode("demo123"));
            demoUser.setName("Demouser");
            demoUser.setFirstname("Demo");
            demoUser.setIsActivated((byte) 1);
            demoUser.setIsEmailConfirmed((byte) 1);
            demoUser.setCreatedAt(LocalDateTime.now());
            demoUser.setUpdatedAt(LocalDateTime.now());
            demoUser.setRoles(Set.of("USER"));

            userRepository.save(demoUser);
            log.info("Created demo user: demo@votianlt.de / demo123");
        }

        log.info("Test users initialization completed.");
    }
}
