package com.smartoffice.config;

import com.smartoffice.user.Role;
import com.smartoffice.user.User;
import com.smartoffice.user.UserRepository;
import com.smartoffice.user.UserStatus;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class DataSeeder implements CommandLineRunner {
    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

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

    @Override
    public void run(String... args) {
        if (userRepository.count() > 0) {
            return;
        }
        userRepository.save(createUser("admin", "管理员", Role.ADMIN));
        userRepository.save(createUser("manager", "部门经理", Role.MANAGER));
        userRepository.save(createUser("employee", "普通员工", Role.EMPLOYEE));
    }

    private User createUser(String username, String fullName, Role role) {
        User user = new User();
        user.setUsername(username);
        user.setPasswordHash(passwordEncoder.encode(username + "123"));
        user.setFullName(fullName);
        user.setRole(role);
        user.setStatus(UserStatus.ACTIVE);
        return user;
    }
}
