package com.ruoyi.web.modules.mall.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.Mockito.never;
import static org.mockito.Mockito.verify;
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.mall.constant.MallConfigConstants;
import com.ruoyi.web.modules.mall.domain.BizMallConfig;
import com.ruoyi.web.modules.mall.dto.MallSettingSaveDTO;
import com.ruoyi.web.modules.mall.mapper.BizMallConfigMapper;
import com.ruoyi.web.modules.mall.service.impl.MallConfigServiceImpl;
import com.ruoyi.web.modules.mall.vo.MallSettingVO;

@ExtendWith(MockitoExtension.class)
class MallConfigServiceImplTest
{
    @Mock
    private BizMallConfigMapper mallConfigMapper;

    @InjectMocks
    private MallConfigServiceImpl mallConfigService;

    @Test
    void getSettings_returnsConfig()
    {
        when(mallConfigMapper.selectById(MallConfigConstants.CONFIG_ID)).thenReturn(config());

        MallSettingVO vo = mallConfigService.getSettings();

        assertEquals(MallConfigConstants.FLAG_YES, vo.getShowSales());
        assertEquals(Boolean.FALSE, vo.getPaidOrderCancelable());
        assertEquals(7, vo.getAutoConfirmDays());
    }

    @Test
    void getAutoConfirmDays_lazyInitWhenMissing()
    {
        when(mallConfigMapper.selectById(MallConfigConstants.CONFIG_ID)).thenReturn(null, config());

        assertEquals(7, mallConfigService.getAutoConfirmDays());
        verify(mallConfigMapper).insert(any(BizMallConfig.class));
    }

    @Test
    void updateSettings_invalidAutoConfirmDays()
    {
        MallSettingSaveDTO dto = validDto();
        dto.setAutoConfirmDays(0);

        assertThrows(ServiceException.class, () -> mallConfigService.updateSettings(dto, "admin"));
        verify(mallConfigMapper, never()).update(any());
    }

    @Test
    void updateSettings_success()
    {
        when(mallConfigMapper.selectById(MallConfigConstants.CONFIG_ID)).thenReturn(config());

        mallConfigService.updateSettings(validDto(), "admin");

        verify(mallConfigMapper).update(any(BizMallConfig.class));
    }

    private MallSettingSaveDTO validDto()
    {
        MallSettingSaveDTO dto = new MallSettingSaveDTO();
        dto.setShowSales(MallConfigConstants.FLAG_YES);
        dto.setAutoConfirmDays(7);
        dto.setAftersaleLimitDays(15);
        dto.setDefaultAvatar("https://example.com/a.png");
        return dto;
    }

    private BizMallConfig config()
    {
        BizMallConfig c = new BizMallConfig();
        c.setConfigId(MallConfigConstants.CONFIG_ID);
        c.setCurrencySymbol(MallConfigConstants.CURRENCY_SYMBOL);
        c.setCurrencyNameCn(MallConfigConstants.CURRENCY_NAME_CN);
        c.setGoodsSnPrefix(MallConfigConstants.GOODS_SN_PREFIX);
        c.setShowSales(MallConfigConstants.FLAG_YES);
        c.setAutoConfirmDays(7);
        c.setAftersaleLimitDays(7);
        c.setDefaultAvatar("https://example.com/default.png");
        return c;
    }
}
