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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Path;
import java.util.Optional;
import java.util.Properties;

import org.junit.jupiter.api.Test;

/**
 * Tests for {@link GuiConfigurationEditorStateFactory}.
 * <p>
 * Verifies that loaded properties are correctly mapped into the editor state, with specific
 * attention to the {@code max.title.length} property mapping.
 */
class GuiConfigurationEditorStateFactoryTest {

    @Test
    void fromPropertiesSnapshot_mapsMaxTitleLengthWhenPresent() {
        Properties props = new Properties();
        props.setProperty("source.folder", "./s");
        props.setProperty("target.folder", "./t");
        props.setProperty("sqlite.file", "./db");
        props.setProperty("prompt.template.file", "./p.txt");
        props.setProperty("ai.provider.active", "claude");
        props.setProperty("max.retries.transient", "3");
        props.setProperty("max.pages", "10");
        props.setProperty("max.text.characters", "1000");
        props.setProperty("max.title.length", "80");
        GuiConfigurationFileSnapshot snapshot =
                new GuiConfigurationFileSnapshot(Path.of("config/application.properties"), props);

        GuiConfigurationEditorState state =
                GuiConfigurationEditorStateFactory.fromPropertiesSnapshot(snapshot, Optional.empty());

        assertEquals("80", state.values().maxTitleLength(),
                "Loaded max.title.length value must be present in the editor state");
        assertTrue(state.hasLoadedFileSnapshot());
        assertFalse(state.isDirty());
    }

    @Test
    void fromPropertiesSnapshot_missingMaxTitleLengthBecomesBlank() {
        Properties props = new Properties();
        props.setProperty("source.folder", "./s");
        props.setProperty("target.folder", "./t");
        props.setProperty("ai.provider.active", "claude");
        // max.title.length intentionally omitted
        GuiConfigurationFileSnapshot snapshot =
                new GuiConfigurationFileSnapshot(Path.of("config/application.properties"), props);

        GuiConfigurationEditorState state =
                GuiConfigurationEditorStateFactory.fromPropertiesSnapshot(snapshot, Optional.empty());

        assertEquals("", state.values().maxTitleLength(),
                "A missing max.title.length property must be mapped to an empty string");
    }

    @Test
    void fromPropertiesSnapshot_blankMaxTitleLengthBecomesBlank() {
        Properties props = new Properties();
        props.setProperty("source.folder", "./s");
        props.setProperty("target.folder", "./t");
        props.setProperty("ai.provider.active", "claude");
        props.setProperty("max.title.length", "   ");
        GuiConfigurationFileSnapshot snapshot =
                new GuiConfigurationFileSnapshot(Path.of("config/application.properties"), props);

        GuiConfigurationEditorState state =
                GuiConfigurationEditorStateFactory.fromPropertiesSnapshot(snapshot, Optional.empty());

        assertEquals("", state.values().maxTitleLength(),
                "A blank max.title.length property must be trimmed to an empty string");
    }
}
