package de.gecheckt.pdf.umbenenner.adapter.in.gui.editor;

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

import java.time.Instant;
import java.util.Optional;

import org.junit.jupiter.api.Test;

/**
 * Tests for {@link GuiMessageEntry}.
 */
class GuiMessageEntryTest {

    @Test
    void fullConstructor_storesAllFields() {
        var now = Instant.now();
        var entry = new GuiMessageEntry(
                GuiMessageSeverity.ERROR,
                "Quellordner fehlt",
                Optional.of("Validierung"),
                now);

        assertThat(entry.severity()).isEqualTo(GuiMessageSeverity.ERROR);
        assertThat(entry.text()).isEqualTo("Quellordner fehlt");
        assertThat(entry.source()).contains("Validierung");
        assertThat(entry.timestamp()).isEqualTo(now);
    }

    @Test
    void nullSourceBecomesEmpty() {
        var entry = new GuiMessageEntry(GuiMessageSeverity.INFO, "text", null, Instant.now());
        assertThat(entry.source()).isEmpty();
    }

    @Test
    void factoryOf_withoutSource_hasEmptySource() {
        var entry = GuiMessageEntry.of(GuiMessageSeverity.INFO, "Konfiguration geladen");
        assertThat(entry.source()).isEmpty();
        assertThat(entry.severity()).isEqualTo(GuiMessageSeverity.INFO);
        assertThat(entry.text()).isEqualTo("Konfiguration geladen");
        assertThat(entry.timestamp()).isNotNull();
    }

    @Test
    void factoryOf_withSource_storesSource() {
        var entry = GuiMessageEntry.of(GuiMessageSeverity.WARNING, "Lange Zeichenzahl", "Validierung");
        assertThat(entry.source()).contains("Validierung");
    }

    @Test
    void rejectsNullSeverity() {
        assertThatNullPointerException()
                .isThrownBy(() -> new GuiMessageEntry(null, "text", Optional.empty(), Instant.now()));
    }

    @Test
    void rejectsNullText() {
        assertThatNullPointerException()
                .isThrownBy(() -> new GuiMessageEntry(GuiMessageSeverity.INFO, null, Optional.empty(), Instant.now()));
    }

    @Test
    void rejectsNullTimestamp() {
        assertThatNullPointerException()
                .isThrownBy(() -> new GuiMessageEntry(GuiMessageSeverity.INFO, "text", Optional.empty(), null));
    }
}
