# src/ai/l2_rule_ai.gd
class_name L2RuleAI
extends BaseAI

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

func _init():
    ai_name = "Rule AI"

func decide(hand: Array, table: Array, current_rank: int, config: RuleConfig) -> HandEvaluator.EvaluatedPlay:
    var moves := MoveGenerator.generate(hand, current_rank, config)
    var last_play := _last_non_pass(table)
    var candidates: Array = []
    for m in moves:
        if m.type == -1:
            continue
        if last_play == null:
            candidates.append(m)
        elif _can_beat(m, last_play, config):
            candidates.append(m)
    if candidates.is_empty():
        return _pass_move()
    var scored := _score_all(candidates, hand.size(), current_rank)
    scored.sort_custom(func(a, b): return a["score"] > b["score"])
    return scored[0]["move"]

func _score_all(moves: Array, hand_size: int, current_rank: int) -> Array:
    var results := []
    for m in moves:
        var score := _score_move(m, hand_size, current_rank)
        results.append({"move": m, "score": score})
    return results

func _score_move(play: HandEvaluator.EvaluatedPlay, hand_size: int, current_rank: int) -> float:
    var score := 0.0
    var remaining := hand_size - play.cards.size()
    score += (27.0 - remaining) / 27.0 * 0.3
    if play.type == _C.TYPE_BOMB or play.type == _C.TYPE_ROCKET:
        score -= 0.2
    elif play.type == _C.TYPE_STRAIGHT_FLUSH:
        score -= 0.1
    score += play.cards.size() * 0.02
    if play.primary_rank >= 14 and remaining > 10:
        score -= 0.1
    if remaining <= 3:
        score += 0.5
    if play.is_pure_bomb:
        score += 0.05
    return score

func _can_beat(play: HandEvaluator.EvaluatedPlay, last: HandEvaluator.EvaluatedPlay, config: RuleConfig) -> bool:
    if play.type == _C.TYPE_ROCKET:
        return true
    if play.type == _C.TYPE_BOMB and last.type != _C.TYPE_ROCKET:
        return RuleEngine.compare_bombs(play, last, config) > 0
    if play.type == last.type:
        return play.primary_rank > last.primary_rank
    return false

func _last_non_pass(table: Array) -> HandEvaluator.EvaluatedPlay:
    for i in range(table.size() - 1, -1, -1):
        if table[i].type != -1:
            return table[i]
    return null

func _pass_move() -> HandEvaluator.EvaluatedPlay:
    var p := HandEvaluator.EvaluatedPlay.new()
    p.type = -1
    p.primary_rank = 0
    return p
