package com.ruoyi.web.modules.content.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.web.modules.content.domain.BizHomeBanner;
import com.ruoyi.web.modules.content.dto.BannerSortDTO;
import com.ruoyi.web.modules.content.service.IBannerService;

/**
 * 平台 Banner 接口测试（BAN-API）
 */
@ExtendWith(MockitoExtension.class)
class BannerControllerTest
{
    @Mock
    private IBannerService bannerService;

    @Spy
    @InjectMocks
    private BannerController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp() throws Exception
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        org.mockito.Mockito.lenient().doReturn("admin").when(controller).getUsername();
    }

    @Test
    void list_returns200() throws Exception
    {
        when(bannerService.selectBannerList(any())).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/agri/banner/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    void getInfo_returns200() throws Exception
    {
        BizHomeBanner row = new BizHomeBanner();
        row.setBannerId(1L);
        row.setBannerName("春耕主推");
        when(bannerService.selectBannerById(1L)).thenReturn(row);

        mockMvc.perform(get("/agri/banner/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.bannerName").value("春耕主推"));
    }

    @Test
    void add_returns200WithData() throws Exception
    {
        Map<String, Object> data = new HashMap<>();
        data.put("bannerId", 1L);
        when(bannerService.insertBanner(any())).thenReturn(data);

        BizHomeBanner body = new BizHomeBanner();
        body.setBannerName("春耕主推");
        body.setBannerImage("/profile/upload/b1.jpg");
        body.setEnableFlag("1");
        body.setSortNo(0);

        mockMvc.perform(post("/agri/banner")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.bannerId").value(1));
    }

    @Test
    void edit_returns200() throws Exception
    {
        Map<String, Object> data = new HashMap<>();
        data.put("bannerId", 1L);
        when(bannerService.updateBanner(any())).thenReturn(data);

        BizHomeBanner body = new BizHomeBanner();
        body.setBannerId(1L);
        body.setBannerName("春耕主推");
        body.setBannerImage("/profile/upload/b1.jpg");
        body.setEnableFlag("0");
        body.setSortNo(1);

        mockMvc.perform(put("/agri/banner")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(body)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    void updateSortNo_returns200() throws Exception
    {
        BannerSortDTO dto = new BannerSortDTO();
        dto.setBannerId(1L);
        dto.setSortNo(10);

        mockMvc.perform(put("/agri/banner/sortNo")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }

    @Test
    void remove_returns200() throws Exception
    {
        when(bannerService.deleteBannerByIds(any(), eq("admin"))).thenReturn("操作成功");

        mockMvc.perform(delete("/agri/banner/1"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
