"""Tests for geometry utilities."""
import pytest
from paper2md.types import BBox
from paper2md.utils.geometry import (
    merge_bboxes, cluster_1d, horizontal_overlap,
    vertical_distance, is_same_line, find_columns,
)


def test_merge_bboxes():
    bbs = [BBox(0, 0, 10, 10), BBox(5, 5, 20, 20)]
    result = merge_bboxes(bbs)
    assert result.x0 == 0
    assert result.y0 == 0
    assert result.x1 == 20
    assert result.y1 == 20


def test_merge_empty_raises():
    with pytest.raises(ValueError):
        merge_bboxes([])


def test_cluster_1d():
    values = [1.0, 1.5, 2.0, 10.0, 10.5, 11.0]
    clusters = cluster_1d(values, gap=3.0)
    assert len(clusters) == 2
    assert len(clusters[0]) == 3
    assert len(clusters[1]) == 3


def test_horizontal_overlap_full():
    a = BBox(0, 0, 100, 10)
    b = BBox(0, 0, 100, 10)
    assert horizontal_overlap(a, b) == 1.0


def test_horizontal_overlap_none():
    a = BBox(0, 0, 10, 10)
    b = BBox(50, 0, 60, 10)
    assert horizontal_overlap(a, b) == 0.0


def test_vertical_distance():
    a = BBox(0, 0, 10, 10)
    b = BBox(0, 20, 10, 30)
    assert vertical_distance(a, b) == 10.0


def test_is_same_line():
    a = BBox(0, 10, 50, 22)
    b = BBox(55, 10, 100, 22)
    assert is_same_line(a, b)


def test_find_columns_single():
    cols = find_columns([100, 200, 150], 600)
    assert len(cols) == 1


def test_find_columns_two():
    left_xs = [50, 60, 70, 80]
    right_xs = [350, 360, 370, 380]
    cols = find_columns(left_xs + right_xs, 600, gap_threshold=20)
    assert len(cols) == 2
