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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
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.AccountManageConstants;
import com.ruoyi.web.modules.account.domain.AccountManageRow;
import com.ruoyi.web.modules.account.dto.AccountPasswordUpdateDTO;
import com.ruoyi.web.modules.account.dto.AccountProfileUpdateDTO;
import com.ruoyi.web.modules.account.mapper.AccountManageMapper;
import com.ruoyi.web.modules.store.domain.BizMerchantAccount;
import com.ruoyi.web.modules.store.mapper.BizMerchantAccountMapper;

@ExtendWith(MockitoExtension.class)
class AccountManageSupportTest
{
    @Mock
    private AccountManageMapper accountManageMapper;

    @Mock
    private BizMerchantAccountMapper merchantAccountMapper;

    @Mock
    private com.ruoyi.system.service.ISysUserService sysUserService;

    @InjectMocks
    private AccountManageSupport support;

    @Test
    void resolveAccountName_prefersNickName()
    {
        assertEquals("李经理", support.resolveAccountName("李经理", "13900139001"));
        assertEquals("13900139001", support.resolveAccountName(null, "13900139001"));
    }

    @Test
    void resolveMobile_merchantAdminUsesLoginName()
    {
        AccountManageRow row = new AccountManageRow();
        row.setMerchantId(1L);
        row.setUserName("13800138000");
        assertEquals("13800138000", support.resolveMobile(row));
    }

    @Test
    void resolveMobile_nonMerchantUsesPhonenumber()
    {
        AccountManageRow row = new AccountManageRow();
        row.setUserName("member001");
        row.setPhonenumber("17700001111");
        assertEquals("17700001111", support.resolveMobile(row));
    }

    @Test
    void validateProfileUpdate_rejectsEmptyAccountName()
    {
        AccountProfileUpdateDTO dto = new AccountProfileUpdateDTO();
        dto.setUserId(10L);
        dto.setAccountName("");
        dto.setMobile("13800138000");

        ServiceException ex = assertThrows(ServiceException.class, () -> support.validateProfileUpdate(dto));
        assertEquals(AccountManageConstants.MSG_ACCOUNT_NAME_REQUIRED, ex.getMessage());
    }

    @Test
    void assertDeletable_rejectsSelf()
    {
        AccountManageRow row = new AccountManageRow();
        row.setUserId(5L);
        when(accountManageMapper.selectByUserId(5L)).thenReturn(row);

        ServiceException ex = assertThrows(ServiceException.class, () -> support.assertDeletable(5L, 5L));
        assertEquals(AccountManageConstants.MSG_CANNOT_DELETE_SELF, ex.getMessage());
    }

    @Test
    void assertDeletable_rejectsWhenMerchantHasShops()
    {
        AccountManageRow row = new AccountManageRow();
        row.setUserId(10L);
        row.setMerchantId(1L);
        row.setShopCount(1);
        when(accountManageMapper.selectByUserId(10L)).thenReturn(row);

        ServiceException ex = assertThrows(ServiceException.class, () -> support.assertDeletable(10L, 99L));
        assertEquals(AccountManageConstants.MSG_HAS_SHOPS, ex.getMessage());
    }

    @Test
    void validatePasswordUpdate_rejectsMismatch()
    {
        AccountPasswordUpdateDTO dto = new AccountPasswordUpdateDTO();
        dto.setUserId(10L);
        dto.setPassword("Pass1234");
        dto.setConfirmPassword("Pass5678");

        ServiceException ex = assertThrows(ServiceException.class, () -> support.validatePasswordUpdate(dto));
        assertEquals(AccountManageConstants.MSG_PASSWORD_MISMATCH, ex.getMessage());
    }
}
