from typing import List
from typing import Dict
from typing import Any
from typing import Tuple


def extract_meetings_by_day(
    formatted_meetings: List[Dict[str, Any]]
) -> Dict[str, List[Tuple[str, str, str, str]]]:
    """
    Transform formatted meetings.

    Input format:
        {
            days: [days]
            start_time:
            end_time:
            location:
        }

    Output format:
        {
            day: [
                [start_time, end_time, location, location_url]
                ...
            ]
        }

    Used in extract_meetings().

    Parameters
    ----------
    formatted_meetings:
        Formatted meetings from extract_formatted_meetings().

    Returns
    -------
    meetings_by_day
    """
    meetings_by_day: Dict[str, List[Tuple[str, str, str, str]]] = {}

    for meeting in formatted_meetings:

        for day in meeting["days"]:

            session = (
                meeting["start_time"],
                meeting["end_time"],
                meeting["location"],
                meeting.get("location_url", ""),
            )

            # if day key already present, append
            if day in meetings_by_day.keys():
                meetings_by_day[day].append(session)
            # otherwise, initialize list
            else:
                meetings_by_day[day] = [session]

    return meetings_by_day