import sys
import json
import csv


def parseSupplyCratesJSONToCSV(jsonFile, csvFile):
    with open(jsonFile, 'r') as file:
        supply_boxes = json.load(file)

    headers = ["box_config_name", "box_friendly_name", "box_class", "box_tooltip", "category", "item_class", "item_name", "quantity"]
    all_rows = []
    for supply_box_name in supply_boxes.keys():
        supply_box_data = supply_boxes[supply_box_name]

        display_name = supply_box_data['displayName']
        tooltip = supply_box_data['tooltip']
        box_class = supply_box_data['boxClass']
        for category in ["items", "backpacks", "weapons", "magazines"]:
            for item in supply_box_data[category]:
                item_class = item[0]
                item_name = item[1]
                item_quantity = item[2]
                row = [supply_box_name, display_name, box_class, tooltip, category, item_class, item_name, item_quantity]
                all_rows.append(row)


    with open(csvFile, 'w', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow(headers)
        writer.writerows(all_rows)

if __name__ == "__main__":
    jsonFile = sys.argv[1]
    csvFile = sys.argv[2]
    parseSupplyCratesJSONToCSV(jsonFile, csvFile)
    print(f"Done! {csvFile} created.")
        