package com.yimeng.okr.service;

import com.yimeng.okr.dto.LoginRequest;
import com.yimeng.okr.entity.SysUser;
import com.yimeng.okr.enums.UserRole;
import com.yimeng.okr.exception.BusinessException;
import com.yimeng.okr.mapper.SysUserMapper;
import com.yimeng.okr.security.JwtTokenProvider;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.password.PasswordEncoder;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class AuthServiceAccountStatusTest {

    @Mock SysUserMapper userMapper;
    @Mock PasswordEncoder passwordEncoder;
    @Mock JwtTokenProvider tokenProvider;
    @InjectMocks AuthService authService;

    @Test
    void loginRejectsDisabledUser() {
        SysUser user = new SysUser();
        user.setId(2L);
        user.setUsername("disabled");
        user.setPasswordHash("hash");
        user.setRole(UserRole.EMPLOYEE);
        user.setStatus("DISABLED");
        when(userMapper.selectOne(any())).thenReturn(user);
        when(passwordEncoder.matches("pw", "hash")).thenReturn(true);

        LoginRequest request = new LoginRequest();
        request.setUsername("disabled");
        request.setPassword("pw");
        BusinessException ex = assertThrows(BusinessException.class, () -> authService.login(request));

        assertTrue(ex.getMessage().contains("禁用"));
        verify(tokenProvider, never()).generateAccessToken(any(), any(), any());
    }
}
