package com.ruoyi.web.modules.account.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
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 com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.account.constant.MemberConstants;
import com.ruoyi.web.modules.account.domain.BizMember;
import com.ruoyi.web.modules.account.dto.MemberLoginDTO;
import com.ruoyi.web.modules.account.dto.MemberRegisterDTO;
import com.ruoyi.web.modules.account.mapper.BizMemberMapper;
import com.ruoyi.web.modules.account.service.impl.MemberAppServiceImpl;
import com.ruoyi.web.modules.content.facade.IMallServiceAgreementFacade;

/**
 * C 端会员 Service 单元测试（MEM-UT）
 */
@ExtendWith(MockitoExtension.class)
class MemberAppServiceImplTest
{
    @Mock
    private BizMemberMapper memberMapper;

    @Mock
    private IMemberSmsService smsService;

    @Mock
    private IMemberTokenService tokenService;

    @Mock
    private IMallServiceAgreementFacade mallServiceAgreementFacade;

    @InjectMocks
    private MemberAppServiceImpl memberAppService;

    @Test
    void register_duplicateMobile_rejected()
    {
        MemberRegisterDTO dto = registerDto();
        when(memberMapper.countByMobile(eq("17700001111"), eq(null))).thenReturn(1);

        assertThrows(ServiceException.class, () -> memberAppService.register(dto));
    }

    @Test
    void login_disabledMember_rejected()
    {
        BizMember member = activeMember();
        member.setStatus(MemberConstants.STATUS_DISABLED);
        member.setPassword(com.ruoyi.common.utils.SecurityUtils.encryptPassword("Pass1234"));
        when(memberMapper.selectByMobile("17700001111")).thenReturn(member);

        MemberLoginDTO dto = new MemberLoginDTO();
        dto.setMobile("17700001111");
        dto.setPassword("Pass1234");

        ServiceException ex = assertThrows(ServiceException.class, () -> memberAppService.login(dto));
        assertEquals("账号已禁用，请联系客服", ex.getMessage());
    }

    private MemberRegisterDTO registerDto()
    {
        MemberRegisterDTO dto = new MemberRegisterDTO();
        dto.setMobile("17700001111");
        dto.setSmsCode("123456");
        dto.setPassword("Pass1234");
        return dto;
    }

    private BizMember activeMember()
    {
        BizMember m = new BizMember();
        m.setMemberId(1L);
        m.setMobile("17700001111");
        m.setStatus(MemberConstants.STATUS_ENABLED);
        return m;
    }
}
