"""
Compatibility algorithm for dating matches in BaoLife.

This module calculates compatibility scores between characters based on:
- Shared interests
- Age compatibility
- Education level
- Wealth/prestige
"""


def calculate_compatibility(player: dict, match: dict) -> int:
    """
    Calculate compatibility score (0-100) between player and potential match

    Factors:
    - Shared interests: +5 per match
    - Age difference: +15 (0-3 years), +5 (4-5 years), -15 (>10 years)
    - Education level: +10 (same), +5 (±1 level)
    - Wealth/prestige: +10 (similar), -5 (very different)

    Args:
        player: Player character dictionary with keys: likes, age_years, prestige, education_level
        match: Potential match character dictionary with same keys

    Returns:
        int: Compatibility score between 0 and 100
    """
    score = 50  # Base compatibility

    # Shared interests
    player_interests = set(player.get('likes', []))
    match_interests = set(match.get('likes', []))
    common = player_interests.intersection(match_interests)
    score += len(common) * 5

    # Age compatibility
    age_diff = abs(player['age_years'] - match['age_years'])
    if age_diff <= 3:
        score += 15
    elif age_diff <= 5:
        score += 5
    elif age_diff > 10:
        score -= 15

    # Education compatibility
    edu_diff = abs(player.get('education_level', 0) - match.get('education_level', 0))
    if edu_diff == 0:
        score += 10
    elif edu_diff == 1:
        score += 5

    # Wealth/prestige compatibility
    prestige_diff = abs(player.get('prestige', 0) - match.get('prestige', 0))
    if prestige_diff < 20:
        score += 10
    elif prestige_diff > 50:
        score -= 5

    return max(0, min(100, score))
