import argparse
import json
import os
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', help='The file to make custom')
parser.add_argument('-o', '--output', help='The output to make custom')
parser.add_argument('-t', '--type', help='The output to make custom')
args = parser.parse_args()
arg_val = args.file
out_val = args.output
out_val = args.output
file_name = ["zero_every_section/rol_empty.json"]
output_name = "master_12json"
if arg_val:
  file_name = []
  file_name.append(arg_val)
#input_files = ["everything/faltantes.json"]
input_files = file_name
# Archivo a salir
if out_val:
  output_name = out_val
#output_file = "array/master_everything_pedido.json"
output_file = output_name
def merge_json_files_to_custom_array(input_files, output_file, unique_key, fields):
    merged_data = {}
    
    # Load and merge data from input files
    for file in input_files:
        with open(file, 'r', encoding='utf-8') as f:
            data = json.load(f)
            for item in data:
                key = item.get(unique_key)
                if key:  # Ensure the key exists
                    merged_data[key] = item
    
    # Create the custom array
    custom_array = []
    for item in merged_data.values():
        custom_entry = [item.get(field, None) for field in fields]
        custom_array.append(custom_entry)
    
    # Write custom array to the output file
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(custom_array, f, ensure_ascii=False, indent=4)
    print(f"Merge completed. Custom array saved to {output_file}")

unique_key = "codigoBarras"  # Replace with the unique key in your JSON
fields = ["seccion_numero", "codigoBarras", "categoria_real", "proveedor", "pasillo", "pedir"]  # Desired fields
if arg_val:
  # Archivo JSON a convertir a master

  # Run the function
  merge_json_files_to_custom_array(input_files, output_file, unique_key, fields)
else:

  for i in range(35):
      file_path = f"everything/pasillo_{i}"
      if os.path.exists(file_path):
        f = [f"{file_path}/result.json"]
        o = f"array/master_pasillo_{i}.json"
        merge_json_files_to_custom_array(f, o, unique_key, fields)

      else:
        continue
