import json

# Read data from symptom_without_id.json
try:
    with open('symptom_without_id.json', 'r', encoding='utf-8') as f:
        data = json.load(f)
except FileNotFoundError:
    print("Error: symptom_without_id.json file not found.")
    exit(1)
except json.JSONDecodeError:
    print("Error: symptom_without_id.json contains invalid JSON.")
    exit(1)

# Function to parse the unit field and set valMin and valMax
def set_min_max(item):
    if item['unit'] in ["0 - 9", "9 - 0"]:
        values = item['unit'].split(' - ')
        item['valMin'] = int(min(values))
        item['valMax'] = int(max(values))

# Add 'id' field to each element and set valMin and valMax
for index, item in enumerate(data, start=2):
    item['id'] = index
    set_min_max(item)

# Write the result to symptomes_parsed.json
with open('symptomes_parsed.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, indent=2, ensure_ascii=False)

print("Data has been read from symptom_without_id.json, modified, and written to symptomes_parsed.json")