package com.njcn.gather.aireport.testdevice.controller;

import com.njcn.gather.aireport.common.storage.AiReportMinioStorageService;
import com.njcn.gather.aireport.testdevice.component.TestDeviceReportStorageService;
import com.njcn.gather.aireport.testdevice.pojo.po.TestDevicePO;
import com.njcn.gather.aireport.testdevice.service.TestDeviceService;
import io.minio.GetObjectResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.util.ReflectionTestUtils;

class TestDeviceControllerTest {

    private final TestDeviceService testDeviceService = Mockito.mock(TestDeviceService.class);
    private final TestDeviceReportStorageService reportStorageService = Mockito.mock(TestDeviceReportStorageService.class);

    @Test
    void previewReportShouldReturnInlinePdfResponse() throws Exception {
        TestDeviceController controller = new TestDeviceController(testDeviceService, reportStorageService);
        TestDevicePO device = new TestDevicePO();
        device.setId("device-1");
        device.setValidityReport("20260626/report.pdf");
        AiReportMinioStorageService.StoredObject storedObject = Mockito.mock(AiReportMinioStorageService.StoredObject.class);
        Mockito.when(testDeviceService.requireNormal("device-1")).thenReturn(device);
        Mockito.when(reportStorageService.resolveDownloadFileName("20260626/report.pdf")).thenReturn("report.pdf");
        Mockito.when(reportStorageService.getObject("20260626/report.pdf")).thenReturn(storedObject);
        Mockito.when(storedObject.getInputStream()).thenReturn(Mockito.mock(GetObjectResponse.class));

        @SuppressWarnings("unchecked")
        ResponseEntity<InputStreamResource> response = (ResponseEntity<InputStreamResource>)
                ReflectionTestUtils.invokeMethod(controller, "buildReportResponse", "device-1", true);

        Assertions.assertNotNull(response);
        Assertions.assertEquals(MediaType.APPLICATION_PDF, response.getHeaders().getContentType());
        Assertions.assertEquals("inline; filename*=UTF-8''report.pdf",
                response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION));
    }
}
