package com.yumu.noveltranslator.bootstrap;

import com.yumu.noveltranslator.adapter.out.persistence.entity.User;
import com.yumu.noveltranslator.adapter.out.persistence.mapper.UserMapper;
import com.yumu.noveltranslator.util.PasswordUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

/**
 * 开发环境自动创建测试账号（仅当不存在时）
 */
@Profile("dev")
@Component
public class DataInitializer implements CommandLineRunner {

    private final UserMapper userMapper;

    public DataInitializer(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void run(String... args) {
        String testEmail = "test@test.com";
        if (userMapper.findByEmail(testEmail) == null) {
            User testUser = new User();
            testUser.setEmail(testEmail);
            testUser.setUsername("测试用户");
            testUser.setPassword(PasswordUtil.hashPassword("test123456"));
            testUser.setUserLevel("FREE");
            testUser.setStatus("ACTIVE");
            userMapper.insert(testUser);
        }
    }
}
