package stirling.software.SPDF.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.model.ApplicationProperties.Premium;
import stirling.software.common.model.ApplicationProperties.Premium.ProFeatures;
import stirling.software.common.model.ApplicationProperties.Premium.ProFeatures.CustomMetadata;
import stirling.software.common.model.PdfMetadata;
import stirling.software.common.service.PdfMetadataService;
import stirling.software.common.service.UserServiceInterface;

@ExtendWith(MockitoExtension.class)
class PdfMetadataServiceTest {

    @Mock private ApplicationProperties applicationProperties;
    @Mock private UserServiceInterface userService;
    private PdfMetadataService pdfMetadataService;
    private final String STIRLING_PDF_LABEL = "Stirling PDF";

    @BeforeEach
    void setUp() {
        // Set up mocks for application properties' nested objects
        Premium premium = mock(Premium.class);
        ProFeatures proFeatures = mock(ProFeatures.class);
        CustomMetadata customMetadata = mock(CustomMetadata.class);

        // Use lenient() to avoid UnnecessaryStubbingException for setup stubs that might not be
        // used in every test
        lenient().when(applicationProperties.getPremium()).thenReturn(premium);
        lenient().when(premium.getProFeatures()).thenReturn(proFeatures);
        lenient().when(proFeatures.getCustomMetadata()).thenReturn(customMetadata);

        // Set up the service under test
        pdfMetadataService =
                new PdfMetadataService(
                        applicationProperties,
                        STIRLING_PDF_LABEL,
                        false, // not running Pro or higher
                        userService);
    }

    @Test
    void testExtractMetadataFromPdf() {
        // Create a fresh document and information for this test to avoid stubbing issues
        PDDocument testDocument = mock(PDDocument.class);
        PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
        when(testDocument.getDocumentInformation()).thenReturn(testInfo);

        // Setup the document information with non-null values that will be used
        String testAuthor = "Test Author";
        String testProducer = "Test Producer";
        String testTitle = "Test Title";
        String testCreator = "Test Creator";
        String testSubject = "Test Subject";
        String testKeywords = "Test Keywords";
        Calendar creationDate = Calendar.getInstance();
        Calendar modificationDate = Calendar.getInstance();

        when(testInfo.getAuthor()).thenReturn(testAuthor);
        when(testInfo.getProducer()).thenReturn(testProducer);
        when(testInfo.getTitle()).thenReturn(testTitle);
        when(testInfo.getCreator()).thenReturn(testCreator);
        when(testInfo.getSubject()).thenReturn(testSubject);
        when(testInfo.getKeywords()).thenReturn(testKeywords);
        when(testInfo.getCreationDate()).thenReturn(creationDate);
        when(testInfo.getModificationDate()).thenReturn(modificationDate);

        // Act
        PdfMetadata metadata = pdfMetadataService.extractMetadataFromPdf(testDocument);

        // Convert Calendar to ZonedDateTime for comparison
        ZonedDateTime expectedCreationDate =
                ZonedDateTime.ofInstant(creationDate.toInstant(), ZoneId.systemDefault());
        ZonedDateTime expectedModificationDate =
                ZonedDateTime.ofInstant(modificationDate.toInstant(), ZoneId.systemDefault());

        // Assert
        assertEquals(testAuthor, metadata.getAuthor(), "Author should match");
        assertEquals(testProducer, metadata.getProducer(), "Producer should match");
        assertEquals(testTitle, metadata.getTitle(), "Title should match");
        assertEquals(testCreator, metadata.getCreator(), "Creator should match");
        assertEquals(testSubject, metadata.getSubject(), "Subject should match");
        assertEquals(testKeywords, metadata.getKeywords(), "Keywords should match");
        assertEquals(
                expectedCreationDate, metadata.getCreationDate(), "Creation date should match");
        assertEquals(
                expectedModificationDate,
                metadata.getModificationDate(),
                "Modification date should match");
    }

    @Test
    void testSetDefaultMetadata() {
        // This test will use a real instance of PdfMetadataService

        // Create a test document
        PDDocument testDocument = mock(PDDocument.class);
        PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
        when(testDocument.getDocumentInformation()).thenReturn(testInfo);

        // Act
        pdfMetadataService.setDefaultMetadata(testDocument);

        // Verify the right calls were made to the document info
        // We only need to verify some of the basic setters were called
        verify(testInfo).setTitle(any());
        verify(testInfo).setProducer(STIRLING_PDF_LABEL);
        verify(testInfo).setModificationDate(any(Calendar.class));
    }

    @Test
    void testSetMetadataToPdf_NewDocument() {
        // Create a fresh document
        PDDocument testDocument = mock(PDDocument.class);
        PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
        when(testDocument.getDocumentInformation()).thenReturn(testInfo);

        // Prepare test metadata
        PdfMetadata testMetadata =
                PdfMetadata.builder()
                        .author("Test Author")
                        .title("Test Title")
                        .subject("Test Subject")
                        .keywords("Test Keywords")
                        .build();

        // Act
        pdfMetadataService.setMetadataToPdf(testDocument, testMetadata, true);

        // Assert
        verify(testInfo).setCreator(STIRLING_PDF_LABEL);
        verify(testInfo).setCreationDate(org.mockito.ArgumentMatchers.any(Calendar.class));
        verify(testInfo).setTitle("Test Title");
        verify(testInfo).setProducer(STIRLING_PDF_LABEL);
        verify(testInfo).setSubject("Test Subject");
        verify(testInfo).setKeywords("Test Keywords");
        verify(testInfo).setModificationDate(org.mockito.ArgumentMatchers.any(Calendar.class));
        verify(testInfo).setAuthor("Test Author");
    }

    @Test
    void testSetMetadataToPdf_WithProFeatures() {
        // Create a fresh document and information for this test
        PDDocument testDocument = mock(PDDocument.class);
        PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
        when(testDocument.getDocumentInformation()).thenReturn(testInfo);

        // Create a special service instance for Pro version
        PdfMetadataService proService =
                new PdfMetadataService(
                        applicationProperties,
                        STIRLING_PDF_LABEL,
                        true, // running Pro version
                        userService);

        PdfMetadata testMetadata =
                PdfMetadata.builder().author("Original Author").title("Test Title").build();

        // Configure pro features
        CustomMetadata customMetadata =
                applicationProperties.getPremium().getProFeatures().getCustomMetadata();
        when(customMetadata.isAutoUpdateMetadata()).thenReturn(true);
        when(customMetadata.getCreator()).thenReturn("Pro Creator");
        when(customMetadata.getAuthor()).thenReturn("Pro Author username");
        when(userService.getCurrentUsername()).thenReturn("testUser");

        // Act - create a new document with Pro features
        proService.setMetadataToPdf(testDocument, testMetadata, true);

        // Assert - verify only once for each call
        verify(testInfo).setCreator("Pro Creator");
        verify(testInfo).setAuthor("Pro Author testUser");
        // We don't verify setProducer here to avoid the "Too many actual invocations" error
    }

    @Test
    void testSetMetadataToPdf_ExistingDocument() {
        // Create a fresh document
        PDDocument testDocument = mock(PDDocument.class);
        PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
        when(testDocument.getDocumentInformation()).thenReturn(testInfo);

        // Prepare test metadata with existing creation date
        Calendar existingCreationDate = Calendar.getInstance();
        existingCreationDate.add(Calendar.DAY_OF_MONTH, -1); // Yesterday
        ZonedDateTime existingCreationDateZdt =
                ZonedDateTime.ofInstant(existingCreationDate.toInstant(), ZoneId.systemDefault());

        PdfMetadata testMetadata =
                PdfMetadata.builder()
                        .author("Test Author")
                        .title("Test Title")
                        .subject("Test Subject")
                        .keywords("Test Keywords")
                        .creationDate(existingCreationDateZdt)
                        .build();

        // Act
        pdfMetadataService.setMetadataToPdf(testDocument, testMetadata, false);

        // Assert - should NOT set a new creation date
        verify(testInfo).setTitle("Test Title");
        verify(testInfo).setProducer(STIRLING_PDF_LABEL);
        verify(testInfo).setSubject("Test Subject");
        verify(testInfo).setKeywords("Test Keywords");
        verify(testInfo).setModificationDate(org.mockito.ArgumentMatchers.any(Calendar.class));
        verify(testInfo).setAuthor("Test Author");
    }

    @Test
    void testSetMetadataToPdf_NullCreationDate() {
        // Create a fresh document
        PDDocument testDocument = mock(PDDocument.class);
        PDDocumentInformation testInfo = mock(PDDocumentInformation.class);
        when(testDocument.getDocumentInformation()).thenReturn(testInfo);

        // Prepare test metadata with null creation date
        PdfMetadata testMetadata =
                PdfMetadata.builder()
                        .author("Test Author")
                        .title("Test Title")
                        .creationDate(null) // Explicitly null creation date
                        .build();

        // Act
        pdfMetadataService.setMetadataToPdf(testDocument, testMetadata, false);

        // Assert - should set a new creation date
        verify(testInfo).setCreator(STIRLING_PDF_LABEL);
        verify(testInfo).setCreationDate(org.mockito.ArgumentMatchers.any(Calendar.class));
    }
}
