extends GutTest

var _config: RuleConfig
const _C = preload("res://src/core/constants.gd")

func before_each():
    _config = RuleConfig.standard()

func _card(orig_id: int, deck_idx: int = 0) -> Card:
    var suit: int
    var rank: int
    if orig_id == 52:
        suit = 4; rank = 15
    elif orig_id == 53:
        suit = 5; rank = 16
    else:
        suit = orig_id % 4
        rank = 2 + (orig_id / 4)
    var c := Card.create(orig_id, suit, rank)
    c.card_id = Card.card_id_from_deck(orig_id, deck_idx)
    return c

func _cards(ids: Array) -> Array[Card]:
    var result: Array[Card] = []
    for spec in ids:
        if spec is int:
            result.append(_card(spec as int))
        elif spec is Array:
            result.append(_card(spec[0] as int, spec[1] as int))
    return result

func test_single():
    var cards := _cards([0])
    var result := HandEvaluator.evaluate(cards, 5, _config)
    assert_eq(result.type, _C.TYPE_SINGLE)

func test_pair():
    # Two cards of same rank from different decks
    var cards: Array[Card] = [_card(0, 0), _card(0, 1)]
    var result := HandEvaluator.evaluate(cards, 5, _config)
    assert_eq(result.type, _C.TYPE_PAIR)

func test_straight():
    # 3,4,5,6,7: orig_ids 4,8,12,16,20 (all from deck 0)
    var cards := _cards([4, 8, 12, 16, 20])
    var result := HandEvaluator.evaluate(cards, 5, _config)
    assert_eq(result.type, _C.TYPE_STRAIGHT)
    assert_eq(result.primary_rank, 7)

func test_not_a_combo():
    var cards := _cards([0, 1, 2])
    var result := HandEvaluator.evaluate(cards, 5, _config)
    assert_eq(result.type, HandEvaluator.INVALID)

func test_bomb():
    # Four 2s: same orig_id from both decks
    var cards: Array[Card] = [_card(0, 0), _card(0, 1), _card(1, 0), _card(1, 1)]
    var result := HandEvaluator.evaluate(cards, 5, _config)
    assert_eq(result.type, _C.TYPE_BOMB)
    assert_eq(result.primary_rank, 2)
    assert_true(result.is_pure_bomb)

func test_rocket():
    var cards: Array[Card] = [_card(52, 0), _card(52, 1), _card(53, 0), _card(53, 1)]
    var result := HandEvaluator.evaluate(cards, 5, _config)
    assert_eq(result.type, _C.TYPE_ROCKET)

func test_evaluator_null_on_empty():
    var cards: Array[Card] = []
    var result := HandEvaluator.evaluate(cards, 5, _config)
    assert_null(result)
