from __future__ import annotations

import pytest

from catan.data import DevelopmentCard
from catan.game import Game, GameConfig, Phase


def _prepare_turn(game: Game) -> None:
    game.phase = Phase.MAIN
    game.has_rolled = True
    game.robber_move_required = False
    game.pending_discards.clear()


def _other_hex(game: Game, exclude: int) -> int:
    return next(h for h in game.board.hexes if h != exclude)


def test_only_one_dev_card_per_turn() -> None:
    game = Game(GameConfig(player_names=["A", "B", "C", "D"]))
    _prepare_turn(game)
    player = game.current_player
    player.dev_cards = [DevelopmentCard.KNIGHT, DevelopmentCard.KNIGHT]
    player.new_dev_cards.clear()

    first_target = _other_hex(game, game.board.robber_hex)
    game.play_knight(first_target, None)

    second_target = _other_hex(game, first_target)
    with pytest.raises(ValueError, match="development card"):
        game.play_knight(second_target, None)


def test_dev_card_limit_resets_next_turn() -> None:
    game = Game(GameConfig(player_names=["A", "B", "C", "D"]))
    _prepare_turn(game)
    player = game.current_player
    player.dev_cards = [DevelopmentCard.KNIGHT]
    player.new_dev_cards.clear()
    target_hex = _other_hex(game, game.board.robber_hex)
    game.play_knight(target_hex, None)
    game.end_turn()

    _prepare_turn(game)
    next_player = game.current_player
    next_player.dev_cards = [DevelopmentCard.KNIGHT]
    next_player.new_dev_cards.clear()
    next_target = _other_hex(game, game.board.robber_hex)
    # Should not raise now that it's a new turn.
    game.play_knight(next_target, None)
