package com.bonus.sharedstation.app.controller;

import com.bonus.sharedstation.InitHeader;
import com.bonus.sharedstation.web.dto.FirstAidInfoDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.nio.charset.StandardCharsets;

/**
 * FileName: FirstAidAppControllerTest
 *
 * @author tqzhang
 * Date: 2025/2/19 上午9:55
 * Description:
 */
@SpringBootTest
@AutoConfigureMockMvc // 自动配置 MockMvc
@ExtendWith(SpringExtension.class) // JUnit 5 annotation
class FirstAidAppControllerTest{
    @Autowired
    private MockMvc mockMvc; // 直接注入 MockMvc

    private HttpHeaders httpHeaders;

    private final String BASE_URL = "/information/firstaidapp";

    @BeforeEach
    public void init() {
        InitHeader initHeader = new InitHeader();
        httpHeaders = new HttpHeaders();
        httpHeaders.addAll( initHeader.getHeader());
    }

    @Test
    void getFirstAidInfoList() throws Exception {
        FirstAidInfoDTO firstAidInfoDTO = new FirstAidInfoDTO();
        firstAidInfoDTO.setCurrentPage(1);
        firstAidInfoDTO.setLimit(1000);
        firstAidInfoDTO.setFirstAidName("");
        firstAidInfoDTO.setPublishStatus("1");
        firstAidInfoDTO.setIsDeleted("0");

        ObjectMapper objectMapper = new ObjectMapper();
        String roleDtoJson = objectMapper.writeValueAsString(firstAidInfoDTO);

        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(BASE_URL + "/getOrderList")
                        .headers(httpHeaders)
                        .content(roleDtoJson)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

        String contentString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
        System.out.println(contentString);

    }

    @Test
    void selectAll() throws Exception {
        FirstAidInfoDTO firstAidInfoDTO = new FirstAidInfoDTO();
        firstAidInfoDTO.setCurrentPage(1);
        firstAidInfoDTO.setLimit(1000);
        firstAidInfoDTO.setFirstAidName("");
        firstAidInfoDTO.setPublishStatus("1");
        firstAidInfoDTO.setIsDeleted("0");

        ObjectMapper objectMapper = new ObjectMapper();
        String roleDtoJson = objectMapper.writeValueAsString(firstAidInfoDTO);

        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(BASE_URL + "/selectAll")
                        .headers(httpHeaders)
                        .content(roleDtoJson)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

        String contentString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
        System.out.println(contentString);
    }

    @Test
    void selectInfoById() throws Exception {
        FirstAidInfoDTO firstAidInfoDTO = new FirstAidInfoDTO();
        firstAidInfoDTO.setId(1);

        ObjectMapper objectMapper = new ObjectMapper();
        String roleDtoJson = objectMapper.writeValueAsString(firstAidInfoDTO);

        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(BASE_URL + "/selectById")
                        .headers(httpHeaders)
                        .content(roleDtoJson)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

        String contentString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
        System.out.println(contentString);
    }
}