"""
Module containing wine match scoring functionality
"""
from typing import Dict, List, Optional, Tuple, Set

def create_kw_match() -> Tuple[Dict[str, int], int]:
    """
    Create a new keyword match data structure
    
    Returns:
        Tuple[Dict[str, int], int]: A tuple containing (matches dict, current score)
    """
    return {}, 0


def set_match_score(matches_data: Tuple[Dict[str, int], int], wine_id: str, score: int) -> Tuple[Dict[str, int], int]:
    """
    Set or update match score for a wine ID
    
    Args:
        matches_data: Tuple containing (matches dict, current score)
        wine_id: The wine ID to score
        score: The score value
        
    Returns:
        Tuple[Dict[str, int], int]: Updated matches data
    """
    matches, current_score = matches_data
    
    if score > current_score:
        # Clear previous matches if this one has a better score
        return {wine_id: score}, score
    elif score == current_score and score > 0:
        # Add to existing matches if same score
        new_matches = dict(matches)
        new_matches[wine_id] = score
        return new_matches, current_score
    else:
        # Return unchanged if score is lower
        return matches, current_score


def get_matches_size(matches_data: Tuple[Dict[str, int], int]) -> int:
    """
    Get number of matches
    
    Args:
        matches_data: Tuple containing (matches dict, current score)
        
    Returns:
        int: Number of matches
    """
    matches, _ = matches_data
    return len(matches)


def get_matches(matches_data: Tuple[Dict[str, int], int]) -> List[str]:
    """
    Get list of match IDs
    
    Args:
        matches_data: Tuple containing (matches dict, current score)
        
    Returns:
        List[str]: List of match IDs
    """
    matches, _ = matches_data
    return list(matches.keys())


def get_first_match(matches_data: Tuple[Dict[str, int], int]) -> Optional[str]:
    """
    Get first match ID if any exists
    
    Args:
        matches_data: Tuple containing (matches dict, current score)
        
    Returns:
        Optional[str]: First match ID or None if no matches
    """
    matches, _ = matches_data
    return next(iter(matches.keys()), None)


def get_score(matches_data: Tuple[Dict[str, int], int]) -> int:
    """
    Get current score
    
    Args:
        matches_data: Tuple containing (matches dict, current score)
        
    Returns:
        int: Current score
    """
    _, score = matches_data
    return score
