package com.walkguide.config;

import com.walkguide.entity.User;
import com.walkguide.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DataSeeder implements CommandLineRunner {

    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) throws Exception {
        if (userRepository.count() == 0) {

            User guardian = User.builder()
                    .email("guardian@walkguide.com")
                    .password(passwordEncoder.encode("guardian123"))
                    .role("ROLE_GUARDIAN")
                    .build();
            userRepository.save(guardian);

            User user = User.builder()
                    .email("user@walkguide.com")
                    .password(passwordEncoder.encode("user123"))
                    .role("ROLE_USER")
                    .build();
            userRepository.save(user);

            System.out.println("DataSeeder: Guardian (" + guardian.getId()
                    + ") dan User (" + user.getId() + ") berhasil dibuat!");
        } else {
            System.out.println("DataSeeder: Database sudah ada data, skip seeding.");
        }
    }
}
