package de.gecheckt.pdf.umbenenner.bootstrap.adapter;

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

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

import org.junit.jupiter.api.Test;

/**
 * Tests für {@link SqliteActiveDatabaseContextAdapter}.
 * <p>
 * Prüft das Setzen, Auslesen und Zurücksetzen des Override-Pfads sowie die Pflicht-Prüfung
 * gegen {@code null}-Argumente.
 */
class SqliteActiveDatabaseContextAdapterTest {

    @Test
    void activeDatabaseOverride_returnsEmpty_initially() {
        SqliteActiveDatabaseContextAdapter adapter = new SqliteActiveDatabaseContextAdapter();
        assertThat(adapter.activeDatabaseOverride()).isEmpty();
    }

    @Test
    void switchActiveDatabase_setsAbsoluteOverride() {
        SqliteActiveDatabaseContextAdapter adapter = new SqliteActiveDatabaseContextAdapter();
        Path target = Path.of("data/foo.sqlite");

        adapter.switchActiveDatabase(target);

        Optional<Path> override = adapter.activeDatabaseOverride();
        assertThat(override).isPresent();
        assertThat(override.get()).isEqualTo(target.toAbsolutePath().normalize());
    }

    @Test
    void switchActiveDatabase_replacesPreviousOverride() {
        SqliteActiveDatabaseContextAdapter adapter = new SqliteActiveDatabaseContextAdapter();
        adapter.switchActiveDatabase(Path.of("first.sqlite"));
        adapter.switchActiveDatabase(Path.of("second.sqlite"));

        assertThat(adapter.activeDatabaseOverride())
                .isPresent()
                .get()
                .isEqualTo(Path.of("second.sqlite").toAbsolutePath().normalize());
    }

    @Test
    void clearOverride_removesPreviouslySetOverride() {
        SqliteActiveDatabaseContextAdapter adapter = new SqliteActiveDatabaseContextAdapter();
        adapter.switchActiveDatabase(Path.of("foo.sqlite"));

        adapter.clearOverride();

        assertThat(adapter.activeDatabaseOverride()).isEmpty();
    }

    @Test
    void switchActiveDatabase_rejectsNull() {
        SqliteActiveDatabaseContextAdapter adapter = new SqliteActiveDatabaseContextAdapter();
        assertThatThrownBy(() -> adapter.switchActiveDatabase(null))
                .isInstanceOf(NullPointerException.class);
    }
}
