package org.raddatz.familienarchiv.document;

import org.junit.jupiter.api.Test;

import java.time.LocalDate;

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

/**
 * The auto-title composition {@code {index} – {dateLabel} – {location}} in isolation.
 * The honest date-label forms themselves are pinned by {@link DocumentTitleFormatterTest}
 * against the shared #666 fixture; here we assert only how the factory composes the
 * three segments and which segments it omits.
 */
class DocumentTitleFactoryTest {

    private final DocumentTitleFactory factory = new DocumentTitleFactory();

    private static Document.DocumentBuilder doc(String index) {
        return Document.builder()
                .originalFilename(index)
                .metaDatePrecision(DatePrecision.UNKNOWN);
    }

    @Test
    void index_only_when_no_date_and_no_location() {
        assertThat(factory.build(doc("C-0029").build())).isEqualTo("C-0029");
    }

    @Test
    void index_and_year_date() {
        Document d = doc("C-0029")
                .documentDate(LocalDate.of(1928, 1, 15))
                .metaDatePrecision(DatePrecision.YEAR)
                .build();
        assertThat(factory.build(d)).isEqualTo("C-0029 – 1928");
    }

    @Test
    void index_date_and_location() {
        Document d = doc("C-0029")
                .documentDate(LocalDate.of(1928, 1, 15))
                .metaDatePrecision(DatePrecision.YEAR)
                .location("Berlin")
                .build();
        assertThat(factory.build(d)).isEqualTo("C-0029 – 1928 – Berlin");
    }

    @Test
    void location_without_date_attaches_directly_to_index() {
        Document d = doc("C-0029").location("Berlin").build();
        assertThat(factory.build(d)).isEqualTo("C-0029 – Berlin");
    }

    @Test
    void unknown_precision_omits_the_date_segment() {
        Document d = doc("C-0029")
                .documentDate(LocalDate.of(1928, 1, 15))
                .metaDatePrecision(DatePrecision.UNKNOWN)
                .build();
        assertThat(factory.build(d)).isEqualTo("C-0029");
    }

    @Test
    void blank_location_is_omitted() {
        Document d = doc("C-0029")
                .documentDate(LocalDate.of(1928, 1, 15))
                .metaDatePrecision(DatePrecision.YEAR)
                .location("   ")
                .build();
        assertThat(factory.build(d)).isEqualTo("C-0029 – 1928");
    }

    @Test
    void bare_document_with_null_index_builds_empty_string_not_npe() {
        // originalFilename is NOT NULL in production; the guard keeps a synthetic/partial entity
        // from tripping StringBuilder(null) with an opaque NPE.
        assertThat(factory.build(Document.builder().build())).isEqualTo("");
    }

    @Test
    void day_precision_renders_the_full_german_label() {
        Document d = doc("C-0029")
                .documentDate(LocalDate.of(1928, 1, 15))
                .metaDatePrecision(DatePrecision.DAY)
                .build();
        assertThat(factory.build(d)).isEqualTo("C-0029 – 15. Januar 1928");
    }
}
