from collections import Counter
from typing import Dict, List
from million.model.message import Message
from million.model.participant import Participant


def count_participations(
        messages: List[Message],
        participants: List[Participant] | None = [],
        threshold: int | None = 0
        ) -> Dict[str, int]:
    """
    Count the number of messages sent by each participant,\n
    you can specify a threshold to return only people having reached that many counts
    """
    participations = dict.fromkeys([p.name for p in participants], 0)
    participations.update(Counter([m.sender_name for m in messages]))
    
    return {k: v for k,v in sorted(participations.items(), key=lambda x: -x[1]) if v >= threshold}

def podium(
        messages: List[Message],
        top: int,
        participants: List[Participant] | None = [],
        ) -> Dict[str, int]:
    """
    Returns the N biggest counters
    """
    cp = count_participations(messages, participants)
    return {k: cp[k] for idx, k in enumerate(cp) if idx < top}