package org.raddatz.familienarchiv.document;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

class DocumentTest {

    @Test
    void getThumbnailUrl_returnsNull_whenThumbnailKeyNull() {
        Document doc = Document.builder()
                .id(UUID.randomUUID())
                .title("Brief")
                .originalFilename("brief.pdf")
                .status(DocumentStatus.UPLOADED)
                .thumbnailKey(null)
                .build();

        assertThat(doc.getThumbnailUrl()).isNull();
    }

    @Test
    void getThumbnailUrl_omitsCacheBuster_whenThumbnailKeyPresentButGeneratedAtNull() {
        UUID id = UUID.fromString("11111111-2222-3333-4444-555555555555");
        Document doc = Document.builder()
                .id(id)
                .title("Brief")
                .originalFilename("brief.pdf")
                .status(DocumentStatus.UPLOADED)
                .thumbnailKey("thumbnails/" + id + ".jpg")
                .thumbnailGeneratedAt(null)
                .build();

        assertThat(doc.getThumbnailUrl())
                .isEqualTo("/api/documents/" + id + "/thumbnail");
    }

    @Test
    void getThumbnailUrl_includesCacheBuster_whenBothKeyAndGeneratedAtPresent() {
        UUID id = UUID.fromString("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee");
        LocalDateTime generatedAt = LocalDateTime.of(2026, 4, 23, 14, 30, 45);
        Document doc = Document.builder()
                .id(id)
                .title("Brief")
                .originalFilename("brief.pdf")
                .status(DocumentStatus.UPLOADED)
                .thumbnailKey("thumbnails/" + id + ".jpg")
                .thumbnailGeneratedAt(generatedAt)
                .build();

        // frontend equivalent: `?v=${encodeURIComponent(doc.thumbnailGeneratedAt)}`
        // where thumbnailGeneratedAt is the ISO-8601 string Jackson serialises.
        // LocalDateTime.toString() produces "2026-04-23T14:30:45"; encodeURIComponent
        // turns ":" into "%3A" but leaves "T" and digits alone.
        String expected = "/api/documents/" + id + "/thumbnail?v=2026-04-23T14%3A30%3A45";
        assertThat(doc.getThumbnailUrl()).isEqualTo(expected);
    }

    @Test
    void thumbnailUrl_isSerialisedToJson_soFrontendReceivesIt() throws Exception {
        UUID id = UUID.fromString("99999999-aaaa-bbbb-cccc-111122223333");
        Document doc = Document.builder()
                .id(id)
                .title("Brief")
                .originalFilename("brief.pdf")
                .status(DocumentStatus.UPLOADED)
                .thumbnailKey("thumbnails/" + id + ".jpg")
                .thumbnailGeneratedAt(LocalDateTime.of(2026, 4, 23, 9, 0, 0))
                .build();

        ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
        String json = mapper.writeValueAsString(doc);

        // Locks the wire contract, not just the Java API: every Document JSON must carry
        // `thumbnailUrl`. Protects against silent breakage if the getter gets renamed,
        // hidden behind @JsonIgnore, or visibility-reduced — any of which would leave the
        // frontend rendering the fallback icon on every surface.
        assertThat(json).contains("\"thumbnailUrl\":\"" + doc.getThumbnailUrl() + "\"");
    }
}
