import sqlite3
import logging


def query_perks(perk_hashes: list[int]) -> list[str]:
    """
    get perk information for all perk hashes passed without maintaining the original order
    :param perk_hashes: the perk's hash values
    :return: list of perk data as List of json-formatted Strings
    :raises IOError: when database query fails
    """
    con = sqlite3.connect('resources/Manifest.content')
    cur = con.cursor()
    try:
        if len(perk_hashes) > 1:
            cur.execute("""
                SELECT
                    json_extract(DestinyInventoryItemDefinition.json, '$')
                FROM
                    DestinyInventoryItemDefinition, json_tree(DestinyInventoryItemDefinition.json, '$')
                WHERE
                    json_tree.key = 'hash' AND json_tree.value IN {}""".format(tuple(map(int, perk_hashes))))
        else:
            cur.execute("""
                SELECT
                    json_extract(DestinyInventoryItemDefinition.json, '$')
                FROM
                    DestinyInventoryItemDefinition, json_tree(DestinyInventoryItemDefinition.json, '$')
                WHERE
                    json_tree.key = 'hash' AND json_tree.value = ?""", (perk_hashes[0], ))
    except sqlite3.Error as e:
        logging.error(f'perk query caused {e}')
        raise IOError
    perks: list[str] = cur.fetchall()
    con.close()

    if len(perks) == 0:
        logging.warning(f'"perk query did not yield db result')

    return perks