package cn.nianxx.thhotel.platform.documentconversion.control;

import static org.mockito.Mockito.verifyNoInteractions;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import cn.nianxx.thhotel.ThHotelApplication;
import cn.nianxx.thhotel.integrations.storage.aliyunoss.service.ObjectStorageService;
import cn.nianxx.thhotel.platform.documentconversion.service.ExcelToPdfConverter;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
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.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest(
        classes = ThHotelApplication.class,
        properties = {
                "document-conversion.enabled=false",
                "document-conversion.access-key=test-document-key"
        })
@AutoConfigureMockMvc
@ActiveProfiles("test")
class DocumentConversionDisabledControllerTest {

    private static final String ENDPOINT = "/api/system/document-conversions/excel-to-pdf";

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ExcelToPdfConverter excelToPdfConverter;

    @MockBean
    private ObjectStorageService objectStorageService;

    @Test
    void shouldRejectUploadWhenDocumentConversionDisabled() throws Exception {
        mockMvc.perform(multipart(ENDPOINT)
                        .file(new MockMultipartFile(
                                "file",
                                "booking-request.xlsx",
                                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                "xlsx-bytes".getBytes(StandardCharsets.UTF_8)))
                        .header("X-TH-Hotel-Document-Conversion-Key", "test-document-key"))
                .andExpect(status().isNotFound())
                .andExpect(jsonPath("$.error_code").value("DOCUMENT_CONVERSION_DISABLED"));

        verifyNoInteractions(excelToPdfConverter, objectStorageService);
    }
}
