import sqlite3


def get_win_rate(num_moves=None, num_trials=None):
    """Get the win rate for a specified configuration of num_moves and num_trials

    Args:
        num_moves (int, optional): The value for num_moves which printed trials must match. Defaults to None.
        num_trials (int, optional): The value for num_trials which printed trials must match. Defaults to None.
    Returns:
        float: The win rate of all the valid trials
    """
    conn = sqlite3.connect("2048_AI_results.db")
    cursor = conn.cursor()
    if num_moves and num_trials:
        cursor.execute("SELECT AVG(did_win) FROM results WHERE num_moves = ? AND num_trials = ?",
                       (num_moves, num_trials))
    else:
        cursor.execute("SELECT AVG(did_win) FROM results")
    win_rate = round(cursor.fetchone()[0] * 100, 2)
    print(f"WIN RATE: {win_rate}%")
    conn.close()
    return win_rate