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

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.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
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.core.page.TableDataInfo;
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.MemberOrderQueryDTO;
import com.ruoyi.web.modules.account.dto.MemberStatusDTO;
import com.ruoyi.web.modules.account.facade.IOrderFacade;
import com.ruoyi.web.modules.account.mapper.BizMemberAddressMapper;
import com.ruoyi.web.modules.account.mapper.BizMemberMapper;
import com.ruoyi.web.modules.account.service.impl.MemberServiceImpl;

@ExtendWith(MockitoExtension.class)
class MemberServiceImplTest
{
    @Mock
    private BizMemberMapper memberMapper;

    @Mock
    private BizMemberAddressMapper addressMapper;

    @Mock
    private IOrderFacade orderFacade;

    @InjectMocks
    private MemberServiceImpl memberService;

    @Test
    void updateStatus_success()
    {
        BizMember member = new BizMember();
        member.setMemberId(1L);
        when(memberMapper.selectById(1L)).thenReturn(member);

        MemberStatusDTO dto = new MemberStatusDTO();
        dto.setStatus(MemberConstants.STATUS_DISABLED);
        memberService.updateMemberStatus(1L, dto, "admin");

        verify(memberMapper).updateStatus(1L, MemberConstants.STATUS_DISABLED, "admin");
    }

    @Test
    void selectMemberOrders_delegatesOrderFacade()
    {
        when(memberMapper.selectById(1L)).thenReturn(new BizMember());
        TableDataInfo table = new TableDataInfo();
        table.setRows(Collections.emptyList());
        table.setTotal(0L);
        when(orderFacade.pageOrdersByMember(eq(1L), any(MemberOrderQueryDTO.class))).thenReturn(table);

        memberService.selectMemberOrders(1L, new MemberOrderQueryDTO());

        verify(orderFacade).pageOrdersByMember(eq(1L), any(MemberOrderQueryDTO.class));
    }

    @Test
    void selectMemberDetail_notFound()
    {
        when(memberMapper.selectById(99L)).thenReturn(null);
        assertThrows(ServiceException.class, () -> memberService.selectMemberDetail(99L));
    }
}
