package cn.novalon.gym.manage.sys.util;

import cn.novalon.gym.manage.sys.core.domain.SysUser;
import cn.novalon.gym.manage.sys.core.domain.SysRole;
import cn.novalon.gym.manage.sys.core.domain.SysLoginLog;
import cn.novalon.gym.manage.sys.core.domain.OperationLog;
import cn.novalon.gym.manage.sys.dto.request.LoginRequest;
import cn.novalon.gym.manage.sys.dto.request.UserRegisterRequest;

import java.time.LocalDateTime;

/**
 * 测试数据工厂类
 * 提供标准化的测试数据创建方法，支持TDD工作流
 */
public class TestDataFactory {
    
    private TestDataFactory() {
        // 工具类，防止实例化
    }
    
    /**
     * 创建测试用户
     */
    public static SysUser createTestUser() {
        SysUser user = new SysUser();
        user.setId(1L);
        user.setUsername("testuser");
        user.setPassword("$2a$12$r8qJ8qJ8qJ8qJ8qJ8qJ8qO"); // BCrypt编码的密码
        user.setEmail("test@example.com");
        user.setStatus(1);
        user.setCreatedAt(LocalDateTime.now());
        return user;
    }
    
    /**
     * 创建禁用状态的用户
     */
    public static SysUser createDisabledUser() {
        SysUser user = createTestUser();
        user.setStatus(0);
        return user;
    }
    
    /**
     * 创建管理员用户
     */
    public static SysUser createAdminUser() {
        SysUser user = createTestUser();
        user.setUsername("admin");
        user.setEmail("admin@example.com");
        return user;
    }
    
    /**
     * 创建用户角色
     */
    public static SysRole createUserRole() {
        SysRole role = new SysRole();
        role.setId(1L);
        role.setRoleKey("ROLE_USER");
        role.setRoleName("普通用户");
        role.setRoleSort(1);
        role.setStatus(1);
        return role;
    }
    
    /**
     * 创建管理员角色
     */
    public static SysRole createAdminRole() {
        SysRole role = new SysRole();
        role.setId(2L);
        role.setRoleKey("ROLE_ADMIN");
        role.setRoleName("管理员");
        role.setRoleSort(2);
        role.setStatus(1);
        return role;
    }
    
    /**
     * 创建登录请求
     */
    public static LoginRequest createLoginRequest() {
        LoginRequest request = new LoginRequest();
        request.setUsername("testuser");
        request.setPassword("password123");
        return request;
    }
    
    /**
     * 创建管理员登录请求
     */
    public static LoginRequest createAdminLoginRequest() {
        LoginRequest request = createLoginRequest();
        request.setUsername("admin");
        return request;
    }
    
    /**
     * 创建注册请求
     */
    public static UserRegisterRequest createRegisterRequest() {
        UserRegisterRequest request = new UserRegisterRequest();
        request.setUsername("newuser");
        request.setPassword("password123");
        request.setEmail("newuser@example.com");
        return request;
    }
    
    /**
     * 创建登录日志
     */
    public static SysLoginLog createLoginLog() {
        SysLoginLog log = new SysLoginLog();
        log.setId(1L);
        log.setUsername("testuser");
        log.setIp("192.168.1.1");
        log.setBrowser("Chrome");
        log.setOs("Windows 10");
        log.setLoginTime(LocalDateTime.now());
        log.setStatus("1");
        return log;
    }
    
    /**
     * 创建操作日志
     */
    public static OperationLog createOperationLog() {
        OperationLog log = new OperationLog();
        log.setId(1L);
        log.setUsername("testuser");
        log.setOperation("创建用户");
        log.setMethod("POST");
        log.setParams("{\"username\":\"testuser\",\"password\":\"password123\"}");
        log.setResult("成功");
        log.setIp("192.168.1.1");
        log.setDuration(100L);
        log.setStatus("1");
        return log;
    }
    
    /**
     * 创建失败的操作日志
     */
    public static OperationLog createFailedOperationLog() {
        OperationLog log = createOperationLog();
        log.setStatus("0");
        log.setErrorMsg("权限不足");
        return log;
    }
}