import json
#some test
#test laptop
# Define the input and output file paths
input_json_path = 'rimond_model_madrid_beautified.json'  # Replace with your input JSON file path
output_json_with_type_path = 'ifc-shape-representation.json'  # JSON with only the specified type
output_json_without_type_path = 'rimond_model_madrid_v3_beautified_new.json'  # JSON without the specified type

def filter_json_by_type(input_json, specified_type):
    with_type = []
    without_type = []

    # Filter the data based on the specified type
    for entry in input_json['data']:
        if entry['type'] == specified_type:
            with_type.append(entry)
        else:
            without_type.append(entry)

    # Create the two output JSON structures
    output_with_type = {
        "type": input_json['type'],
        "version": input_json['version'],
        "schemaIdentifier": input_json['schemaIdentifier'],
        "originatingSystem": input_json['originatingSystem'],
        "preprocessorVersion": input_json['preprocessorVersion'],
        "timeStamp": input_json['timeStamp'],
        "data": with_type
    }

    output_without_type = {
        "type": input_json['type'],
        "version": input_json['version'],
        "schemaIdentifier": input_json['schemaIdentifier'],
        "originatingSystem": input_json['originatingSystem'],
        "preprocessorVersion": input_json['preprocessorVersion'],
        "timeStamp": input_json['timeStamp'],
        "data": without_type
    }

    return output_with_type, output_without_type

def main(specified_type):
    # Read the input JSON file
    with open(input_json_path, 'r') as file:
        input_json = json.load(file)

    # Filter the JSON data
    output_with_type, output_without_type = filter_json_by_type(input_json, specified_type)

    # Write the filtered JSON data to new files
    with open(output_json_with_type_path, 'w') as file:
        json.dump(output_with_type, file, indent=4)

    with open(output_json_without_type_path, 'w') as file:
        json.dump(output_without_type, file, indent=4)

    print(f"Successfully filtered the JSON data based on type '{specified_type}' and saved to:")
    print(f"- {output_json_with_type_path}")
    print(f"- {output_json_without_type_path}")

# Example usage
specified_type = "IfcShapeRepresentation"  # Replace with the type you want to filter by
main(specified_type)
