import json
import sqlparse

def reformat_sql(sql_item):
    if isinstance(sql_item, list):
        sql_str = " ".join(sql_item)
    else:
        sql_str = sql_item
        
    formatted = sqlparse.format(sql_str, reindent=True, keyword_case='upper')
    # Filter out empty lines and ensure we return a list of strings
    return [line.rstrip() for line in formatted.split('\n') if line.strip()]

def process_file(filename):
    with open(filename, 'r') as f:
        data = json.load(f)
    
    for suite in data:
        for test in suite.get("tests", []):
            if "expect" in test and "sql" in test["expect"]:
                if test["expect"]["sql"] is None:
                    continue
                new_sql = []
                for q in test["expect"]["sql"]:
                    new_sql.append(reformat_sql(q))
                test["expect"]["sql"] = new_sql
                
    with open(filename, 'w') as f:
        json.dump(data, f, indent=4)

if __name__ == "__main__":
    process_file("fixtures/queryer.json")
    process_file("fixtures/merger.json")
