import csv
from neo4j import GraphDatabase
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Retrieve Neo4j connection details from environment variables
NEO4J_URI = os.getenv('NEO4J_URI')
NEO4J_USER = os.getenv('NEO4J_USER')
NEO4J_PASSWORD = os.getenv('NEO4J_PASSWORD')

if not NEO4J_URI or not NEO4J_USER or not NEO4J_PASSWORD:
    raise ValueError("Neo4j connection details not found in .env file")

TEST_FLAG = False

# Function to create or update a bill node in Neo4j using MERGE
def merge_bill_node(driver, bill):
    with driver.session() as session:
        query = """
            MERGE (b:Bill {id: $id})
            ON CREATE SET b += $properties
            ON MATCH SET b += $properties
            RETURN b
        """

        # Filter out empty or None values and ensure keys are strings
        properties = {str(key): value for key, value in bill.items() if value is not None and value != ""}

        if TEST_FLAG:
            print(f"MERGE (b:Bill {{id: '{bill['number']}'}}) ON CREATE SET b += ${properties} ON MATCH SET b += ${properties}")
        else:
            result = session.run(query, id=bill['number'], properties=properties)
            return result.single()

# Read the CSV file and create/update nodes in Neo4j
def read_csv_and_merge_nodes(file_path):
    driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))

    try:
        with open(file_path, mode='r', newline='', encoding='utf-8') as csvfile:
            reader = csv.DictReader(csvfile, delimiter='^')

            for row in reader:
                bill = {key: value.strip() if isinstance(value, str) else value for key, value in row.items()}

                # Debugging print statement to check keys and values
                print(f"Processing row with keys: {bill.keys()} and values: {bill.values()}")

                result = merge_bill_node(driver, bill)
                if not TEST_FLAG:
                    print(f"Created/Updated node: {result}")

    finally:
        if not TEST_FLAG:
            driver.close()

# Path to the CSV file
csv_file_path = 'bills.csv'

# Run the function
read_csv_and_merge_nodes(csv_file_path)