import itertools


def make_call_summary_report(calls_list, users, for_graphviz=False):
    """
    Makes a graphical or textual report of who has been on a call with who.

    Only considers calls initiated by one of the tracked users.

    Parameters
    ----------
    calls_list: list
        Calls to look through (perhaps representing a section of history)
    users: list
        List of users to consider.
    for_graphviz: boolean
        A flag indicating this report should be for graphviz dot tool.  
        If false, prepares a human-readable text report section.
    """
    lines = [
        "", "Section C - Calls Summary - Who talks to whom? (group-initiated calls only)", ""]
    summary_of_each_call = [
        (users[call['user']]['real_name'],
         call['duration_m'],
         [users[participant]['real_name']
          for participant in call['participants'] if participant in users]
         )
        for call in calls_list if call['user'] in users]  # call['user'] is participant

    if for_graphviz:
        # list each user as node
        # list each call as a list of edges between its participants
        lines = ["graph CallNetwork {"]
        for uid in users:
            lines.append("%s;" % users[uid]['real_name'])
        for (_, _, participants) in summary_of_each_call:
            for (f, t) in itertools.combinations(participants, 2):
                lines.append("%s -- %s;" % (f, t))

        lines.append("}")

    else:
        for (starter, duration, participants) in summary_of_each_call:
            lines.append("Call of duration %dm started by %s, \n\twith participants %s"
                         % (duration, starter, ", ".join(participants)))

    return "\n".join(lines)