"""
Mock storage implementation for testing.

Provides in-memory storage that mimics database behavior without requiring
a real database connection.
"""
import copy
from typing import Dict, Optional, List, Tuple


class MockGameStorage:
    """
    Mock implementation of IGameStorage for testing.

    Stores player data in memory instead of database.
    """

    def __init__(self):
        """Initialize mock storage with empty dictionary."""
        self.games: Dict[str, any] = {}
        self.save_count = 0
        self.load_count = 0

    def save_game(self, player) -> bool:
        """
        Save a game to mock storage.

        Args:
            player: The player object to save

        Returns:
            True if successful
        """
        try:
            # Deep copy to prevent test mutations
            self.games[player.userID] = copy.deepcopy(player)
            self.save_count += 1
            return True
        except Exception as e:
            print(f"Mock save error: {e}")
            return False

    def load_game(self, user_id: str):
        """
        Load a game from mock storage.

        Args:
            user_id: The user ID to load

        Returns:
            Deep copy of player object if found, None otherwise
        """
        self.load_count += 1
        if user_id in self.games:
            # Deep copy to prevent test mutations
            return copy.deepcopy(self.games[user_id])
        return None

    def load_games(self) -> List[Tuple]:
        """
        Load all games from mock storage.

        Returns:
            List of (user_id,) tuples
        """
        return [(user_id,) for user_id in self.games.keys()]

    def game_exists(self, user_id: str) -> bool:
        """
        Check if a game exists in mock storage.

        Args:
            user_id: User ID to check

        Returns:
            True if game exists
        """
        return user_id in self.games

    def clear(self):
        """Clear all saved games (for test cleanup)."""
        self.games.clear()
        self.save_count = 0
        self.load_count = 0

    def get_save_count(self) -> int:
        """Get number of times save_game was called."""
        return self.save_count

    def get_load_count(self) -> int:
        """Get number of times load_game was called."""
        return self.load_count
