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

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
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.web.modules.mall.facade.IMallConfigFacade;
import com.ruoyi.web.modules.order.constant.OrderConstants;
import com.ruoyi.web.modules.order.domain.BizOrder;
import com.ruoyi.web.modules.order.support.OrderAppSupport;

@ExtendWith(MockitoExtension.class)
class OrderAppSupportAftersaleTest
{
    @Mock
    private IMallConfigFacade mallConfigFacade;

    @InjectMocks
    private OrderAppSupport orderAppSupport;

    @Test
    void isAftersaleExpired_withinLimit()
    {
        when(mallConfigFacade.getAftersaleLimitDays()).thenReturn(7);
        BizOrder order = completedOrder(daysAgo(3));
        assertFalse(orderAppSupport.isAftersaleExpired(order));
    }

    @Test
    void isAftersaleExpired_beyondLimit()
    {
        when(mallConfigFacade.getAftersaleLimitDays()).thenReturn(7);
        BizOrder order = completedOrder(daysAgo(10));
        assertTrue(orderAppSupport.isAftersaleExpired(order));
    }

    private BizOrder completedOrder(Date finishTime)
    {
        BizOrder order = new BizOrder();
        order.setOrderStatus(OrderConstants.STATUS_COMPLETED);
        order.setFinishTime(finishTime);
        return order;
    }

    private Date daysAgo(int days)
    {
        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_MONTH, -days);
        return cal.getTime();
    }
}
