from typing import Any

from rich.table import Table
from textual.widgets import RichLog

from llm_werewolf.core.game_state import GameState


class PlayerPanel(RichLog):
    """Widget displaying the list of players and their status."""

    def __init__(self, *args: Any, **kwargs: Any) -> None:  # noqa: ANN401
        """Initialize the player panel."""
        super().__init__(*args, **kwargs)
        self.game_state: GameState | None = None

    def set_game_state(self, game_state: GameState) -> None:
        """Set the game state to display.

        Args:
            game_state: The current game state.
        """
        self.game_state = game_state
        self.refresh_display()

    def refresh_display(self) -> None:
        """Refresh the display with current game state."""
        if not self.game_state:
            return

        # Clear previous content
        self.clear()

        # Check if there are any human players in the game
        has_human_player = any(p.ai_model == "human" for p in self.game_state.players)

        # Create table without title
        table = Table(
            show_header=True,
            header_style="bold magenta",
            box=None,
            padding=(0, 1),
            collapse_padding=True,
        )
        table.add_column("Name", style="cyan", no_wrap=True)
        table.add_column("Model", style="blue", no_wrap=True)
        table.add_column("Status", style="green", no_wrap=True)
        table.add_column("Role", style="yellow", no_wrap=True)

        for player in self.game_state.players:
            # Determine status icon
            if player.is_alive():
                status_icon = "✓"
                status_style = "green"
            else:
                status_icon = "✗"
                status_style = "red"

            # Role display logic:
            # - If there's a human player: hide roles (show "?") unless dead
            # - If no human player: show all roles (spectator/testing mode)
            if has_human_player:
                role_display = player.get_role_name() if not player.is_alive() else "?"
            else:
                role_display = player.get_role_name()

            # Add special status indicators
            status_text = status_icon
            if player.has_status("protected"):
                status_text += " 🛡️"
            if player.has_status("poisoned"):
                status_text += " ☠️"
            if player.has_status("marked"):
                status_text += " 🔴"
            if player.has_status("lover"):
                status_text += " ❤️"

            table.add_row(
                player.name,
                player.ai_model,
                f"[{status_style}]{status_text}[/{status_style}]",
                role_display,
            )

        # Write table to RichLog
        self.write(table)

    def on_mount(self) -> None:
        """Called when widget is mounted."""
        self.refresh_display()
