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_can_play_fresh_round():
    var hand: Array[Card] = [_card(0), _card(4)]
    var table: Array = []
    var play := HandEvaluator.evaluate(_cards([0]), 5, _config)
    var result := RuleEngine.can_play(hand, play, table, -1, 5, _config)
    assert_true(result.ok)

func test_pure_bomb_beats_mixed():
    var pure := HandEvaluator.EvaluatedPlay.new()
    pure.type = _C.TYPE_BOMB
    pure.primary_rank = 10
    pure.is_pure_bomb = true
    var mixed := HandEvaluator.EvaluatedPlay.new()
    mixed.type = _C.TYPE_BOMB
    mixed.primary_rank = 10
    mixed.is_pure_bomb = false
    assert_eq(RuleEngine.compare_bombs(pure, mixed, _config), 1)
    assert_eq(RuleEngine.compare_bombs(mixed, pure, _config), -1)

func test_rocket_beats_bomb():
    var rocket := HandEvaluator.EvaluatedPlay.new()
    rocket.type = _C.TYPE_ROCKET
    rocket.primary_rank = 999
    var bomb := HandEvaluator.EvaluatedPlay.new()
    bomb.type = _C.TYPE_BOMB
    bomb.primary_rank = 14
    assert_eq(RuleEngine.compare(rocket, bomb, _config), 1)

func test_same_type_higher_beats():
    var low := HandEvaluator.EvaluatedPlay.new()
    low.type = _C.TYPE_SINGLE; low.primary_rank = 3
    var high := HandEvaluator.EvaluatedPlay.new()
    high.type = _C.TYPE_SINGLE; high.primary_rank = 14
    assert_eq(RuleEngine.compare(high, low, _config), 1)

func test_cannot_play_card_not_in_hand():
    var hand: Array[Card] = [_card(4)]
    var play := HandEvaluator.evaluate(_cards([0]), 5, _config)  # card 0 not in hand
    var result := RuleEngine.can_play(hand, play, [], -1, 5, _config)
    assert_false(result.ok)

func test_team_partner():
    var t := Actions.Team.create_team(0, 0, 2)
    assert_eq(t.teammate_of(0), 2)
    assert_eq(t.teammate_of(2), 0)
    assert_true(t.contains(0))
