import json
from rdflib import Graph, Literal, Namespace, RDF, URIRef, XSD
import os

# Define the namespaces
SINCERE = Namespace("http://sincere.org/s1#")


# Function to convert JSON to RDF
def json_to_rdf(json_data, rdf_file_path):
    # Create an RDF graph
    g = Graph()

    # Bind the SINCERE namespace
    g.bind("sincere", SINCERE)

    # Process each entry in the data
    for i, entry in enumerate(json_data['data'], start=1):
        channel_id = entry['channel_id']
        data_value = entry['data_value']
        measurement = entry['measurement']
        time = entry['time']

        channel_uri = URIRef(f"http://sincere.org/s1/resource/channel_{channel_id}")
        measurement_uri = URIRef(f"http://sincere.org/s1/resource/measurement_{channel_id}_{i}")

        g.add((channel_uri, RDF.type, SINCERE.Channel))
        g.add((channel_uri, SINCERE.hasMeasurement, measurement_uri))

        g.add((measurement_uri, RDF.type, SINCERE.Measurement))
        g.add((measurement_uri, SINCERE.measurement, Literal(measurement, datatype=XSD.string)))
        g.add((measurement_uri, SINCERE.time, Literal(time, datatype=XSD.string)))
        g.add((measurement_uri, SINCERE.value, Literal(data_value, datatype=XSD.float)))

    # Serialize the graph in Turtle format and save to a file
    g.serialize(destination=rdf_file_path, format="turtle")


# Function to read all JSON files from a folder and process each one
def process_json_files(input_dir, output_dir):
    # Ensure the output directory exists
    os.makedirs(output_dir, exist_ok=True)

    # Iterate over all files in the input directory
    count = 1
    for file_name in os.listdir(input_dir):
        file_path = os.path.join(input_dir, file_name)

        if os.path.isfile(file_path) and file_path.endswith('.json'):
            try:
                # Read JSON data from the file
                with open(file_path, 'r') as file:
                    json_data = json.load(file)

                # Generate an RDF file path based on the file name
                rdf_file_name = "output_channel_" + str(count) + ".ttl"
                rdf_file_path = os.path.join(output_dir, rdf_file_name)

                # Convert JSON to RDF and save to file
                json_to_rdf(json_data, rdf_file_path)
                print(f"Processed {file_name} -> {rdf_file_path}")
                count += 1
            except json.JSONDecodeError as e:
                print(f"Error decoding JSON in file {file_name}: {e}")
            except PermissionError:
                print(f"Permission denied for file {file_name}")


# Example usage
input_dir = 'C:/Users/USER/PycharmProjects/sincere/meteorogical_data/input_json_file'  # Replace with your input directory containing JSON files
output_dir = 'C:/Users/USER/PycharmProjects/sincere/meteorogical_data/output_json_file'  # Replace with your desired output directory for RDF files

process_json_files(input_dir, output_dir)
