from typing import List, Dict, Tuple
import sqlite3

E6_DB = 'e6_database.db'

# Helper function to run queries
def run_query(db: str, db_query: str, args: tuple = None) -> List[tuple]:
    """Return the results of running query db_query on database with name db.

    Optional query arguments for can be included in args.
    """
    
    con = sqlite3.connect(db)
    cur = con.cursor()
    
    if args is None: 
        cur.execute(db_query)
    else:
        cur.execute(db_query, args)

    data = cur.fetchall()
    cur.close()
    con.close()
    return data

# ---- Exercise 6 begins from here ---- #

def get_book_cnt(db: str, author_name: str) -> List[tuple]:
    """Return a list of the author_name and book count written by author with
    author_name according to the information in the Books table in database db.

    >>> get_book_cnt(E6_DB, 'Jose Saramago')
    [('Jose Saramago', 3)]
    >>> get_book_cnt(E6_DB, 'Aesopus')
    [('Aesopus', 1)]
    >>> get_book_cnt(E6_DB, 'Jane Austin')
    []
    """ 
    # Add your code here
    # HINT: You will need to you an SQL Aggregate Function


# Please note that the docstring example in the get_book_cnt_per_author
# only checks for part of the returned list due to space constraints.
def get_book_cnt_per_author(db: str) -> List[tuple]:
    """Return a list of (author, book count written by author) for 
    all authors in the Books table in database db, sorted by author
    (in ascending order). 

    >>> author_cnt_list = get_book_cnt_per_author(E6_DB)
    >>> author_cnt_list[0]
    ('A.L. Kennedy', 2)
    >>> author_cnt_list[-1] 
    ('emile Zola', 5)
    >>> author_cnt_list[-2] 
    ('Zora Neale Hurston', 1)
    """ 

    # Add your code here


# Please note that the docstring example in the create_author_dict function
# only checks for two entries of the dictionary due to space constraints.
def create_author_dict(db: str) -> Dict[str, List[str]]:
    """Return a dictionary that maps each author to the books they have written
    according to the information in the Books table of the database
    with name db. 

    >>> author_dict = create_author_dict(E6_DB)
    >>> author_dict['Isaac Asimov'].sort()
    >>> author_dict['Isaac Asimov']
    ['Foundation', 'I Robot']
    >>> author_dict['Maya Angelou']
    ['I Know Why the Caged Bird Sings']
    """  
    # Add your code here

    

if __name__ == "__main__":
    # Your main block will not be marked. We will be importing your code for marking.
    # Feel free to use it, if you choose to.  But make sure you don't have
    # any syntax errors when running the code!
    
    pass    
