package org.raddatz.familienarchiv.document.annotation;

import org.junit.jupiter.api.Test;

import java.util.List;

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

class PolygonConverterTest {

    private final PolygonConverter converter = new PolygonConverter();

    @Test
    void convertToDatabaseColumn_returnsNull_whenPolygonIsNull() {
        assertThat(converter.convertToDatabaseColumn(null)).isNull();
    }

    @Test
    void convertToDatabaseColumn_returnsJsonArray_whenPolygonIsValid() {
        List<List<Double>> polygon = List.of(
                List.of(0.1, 0.2),
                List.of(0.9, 0.2),
                List.of(0.9, 0.8),
                List.of(0.1, 0.8));

        String json = converter.convertToDatabaseColumn(polygon);

        assertThat(json).isEqualTo("[[0.1,0.2],[0.9,0.2],[0.9,0.8],[0.1,0.8]]");
    }

    @Test
    void convertToEntityAttribute_returnsNull_whenJsonIsNull() {
        assertThat(converter.convertToEntityAttribute(null)).isNull();
    }

    @Test
    void convertToEntityAttribute_returnsNull_whenJsonIsEmpty() {
        assertThat(converter.convertToEntityAttribute("")).isNull();
    }

    @Test
    void convertToEntityAttribute_returnsPolygon_whenJsonIsValid() {
        String json = "[[0.1,0.2],[0.9,0.2],[0.9,0.8],[0.1,0.8]]";

        List<List<Double>> polygon = converter.convertToEntityAttribute(json);

        assertThat(polygon).hasSize(4);
        assertThat(polygon.get(0)).containsExactly(0.1, 0.2);
        assertThat(polygon.get(3)).containsExactly(0.1, 0.8);
    }

    @Test
    void roundTrip_preservesValues() {
        List<List<Double>> original = List.of(
                List.of(0.12, 0.08),
                List.of(0.88, 0.09),
                List.of(0.87, 0.14),
                List.of(0.11, 0.13));

        String json = converter.convertToDatabaseColumn(original);
        List<List<Double>> restored = converter.convertToEntityAttribute(json);

        assertThat(restored).isEqualTo(original);
    }
}
