package com.cf.imes.module.system.controller.admin.funds.productpromotion;

import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.test.core.ut.BaseWebUnitTest;
import com.cf.imes.module.system.controller.admin.funds.productpromotion.vo.ProductPromotionPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.productpromotion.vo.ProductPromotionSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.productpromotion.ProductPromotionDO;
import com.cf.imes.module.system.enums.pay.DurationUnitEnum;
import com.cf.imes.module.system.service.funds.productpromotion.ProductPromotionService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import java.time.LocalDateTime;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @author Gqr
 * @since 2025/10/27 9:29
 */
@WebMvcTest(controllers = ProductPromotionController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
class ProductPromotionControllerTest extends BaseWebUnitTest {
    @Autowired
    private MockMvc mockMvc;

    @MockitoBean
    private ProductPromotionService productPromotionService;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    void testCreateRechargeActive() throws Exception {
        ProductPromotionSaveReqVO vo = new ProductPromotionSaveReqVO();
        vo.setActivityName("充值活动");
        vo.setProductId(1L);
        vo.setPurchaseDuration(1);
        vo.setPurchaseDurationUnit(DurationUnitEnum.YEAR.getCode());
        vo.setGiftDuration(1);
        vo.setGiftDurationUnit(DurationUnitEnum.DAY.getCode());
        LocalDateTime now = LocalDateTime.now();
        vo.setStartTime(now);
        vo.setEndTime(now);

        // mock Service 返回
        when(productPromotionService.create(any())).thenReturn(100L);

        mockMvc.perform(post("/system/product/promotion")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(vo)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data").value(100));

        // 验证 Service 调用
        verify(productPromotionService, times(1)).create(any());
    }

    @Test
    void testUpdateRechargeActive() throws Exception {
        ProductPromotionSaveReqVO vo = new ProductPromotionSaveReqVO();
        vo.setActivityName("更新充值活动");
        vo.setProductId(2L);
        vo.setPurchaseDuration(2);
        vo.setPurchaseDurationUnit(DurationUnitEnum.YEAR.getCode());
        vo.setGiftDuration(2);
        vo.setGiftDurationUnit(DurationUnitEnum.DAY.getCode());
        LocalDateTime now = LocalDateTime.now();
        vo.setStartTime(now);
        vo.setEndTime(now);

        // mock Service 返回
        doNothing().when(productPromotionService).update(any());

        mockMvc.perform(put("/system/product/promotion")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(vo)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data").value(true));

        // 验证 Service 调用
        verify(productPromotionService, times(1)).update(any());
    }

    @Test
    void testDeleteRechargeActive() throws Exception {
        Long id = 3L;
        doNothing().when(productPromotionService).delete(anyLong());

        mockMvc.perform(delete("/system/product/promotion/{id}", id))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data").value(true));

        verify(productPromotionService, times(1)).delete(id);
    }

    @Test
    void testGetRechargeActive() throws Exception {
        Long id = 123L;
        ProductPromotionDO mockDO = new ProductPromotionDO()
                .setId(id);
        when(productPromotionService.get(id)).thenReturn(mockDO);

        mockMvc.perform(get("/system/product/promotion/{id}", id))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.id").value(123L));

        verify(productPromotionService, times(1)).get(id);
    }

    @Test
    void testGetRechargeActivePage() throws Exception {
        ProductPromotionPageReqVO reqVO = new ProductPromotionPageReqVO();
        PageResult<ProductPromotionDO> mockPage = new PageResult<>();
        mockPage.setTotal(1L);
        mockPage.setList(List.of(new ProductPromotionDO().setId(1L)));
        when(productPromotionService.getPage(any())).thenReturn(mockPage);

        mockMvc.perform(get("/system/product/promotion/page")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(reqVO)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.list[0].id").value(1L));

        verify(productPromotionService, times(1)).getPage(any());
    }
}
