"""geometry.led_boards（基板ラベル判定と基板内の並び順）の仕様ベーステスト．

仕様出典:
- 基板ラベル: 既存規則（|Y| > |X| なら Y 符号で bottom/top，そうでなければ X 符号で right/left）
- ID `led_<board>_<k>`，並びは bottom/top が X 昇順，right/left が Y 昇順
- 出力順は bottom → right → left → top（scene_baseline.yaml の lights の並び）
"""

from __future__ import annotations

import pytest

from sm_tias_lightsim.geometry.led_boards import (
    BOARD_ORDER,
    board_order_index,
    board_sort_axis,
    classify_board_position,
    order_leds_on_board,
)


@pytest.mark.parametrize(("center", "label"), [
    ((0.0, -71.0, -66.0), "bottom"),
    ((3.8, -71.0, -64.0), "bottom"),
    ((0.0, 71.0, -66.0), "top"),
    ((66.0, -11.0, -66.0), "right"),
    ((-66.0, -18.8, -66.0), "left"),
    ((10.0, 10.0, 0.0), "right"),     # |Y| == |X| は左右軸
    ((-10.0, 10.0, 0.0), "left"),
])
def test_classify_board_position(center, label):
    assert classify_board_position(center) == label


def test_board_order_matches_baseline():
    assert BOARD_ORDER == ("bottom", "right", "left", "top")
    assert [board_order_index(b) for b in BOARD_ORDER] == [0, 1, 2, 3]
    assert board_order_index("unknown") == len(BOARD_ORDER)


def test_bottom_and_top_sort_by_x():
    centers = [(17.6, -71, 0), (-10.0, -71, 0), (9.2, -60, 0), (-1.6, -80, 0)]
    for label in ("bottom", "top"):
        assert order_leds_on_board(label, centers) == [1, 3, 2, 0]


def test_right_and_left_sort_by_y():
    centers = [(66, 2.6, 0), (60, -24.9, 0), (70, -5.7, 0)]
    for label in ("right", "left"):
        assert order_leds_on_board(label, centers) == [1, 2, 0]


def test_order_is_stable_for_ties():
    centers = [(1.0, 0, 0), (1.0, 5, 0), (0.0, 0, 0)]
    assert order_leds_on_board("bottom", centers) == [2, 0, 1]


def test_order_empty():
    assert order_leds_on_board("left", []) == []


def test_unknown_label_raises():
    with pytest.raises(ValueError):
        board_sort_axis("front")
    with pytest.raises(ValueError):
        order_leds_on_board("front", [(0, 0, 0)])
