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 person node in Neo4j using MERGE
def merge_person_node(driver, member):
    with driver.session() as session:
        query = """
            MERGE (p:Person {bioguideId: $bioguideId})
            ON CREATE SET p += $properties
            ON MATCH SET p += $properties
            RETURN p
        """

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

        if TEST_FLAG:
            print(f"MERGE (p:Person {{bioguideId: '{member['bioguideId']}'}}) ON CREATE SET p += ${properties} ON MATCH SET p += ${properties}")
        else:
            result = session.run(query, bioguideId=member['bioguideId'], 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:
                member = {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: {member.keys()} and values: {member.values()}")

                result = merge_person_node(driver, member)
                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 = 'members.csv'

# Run the function
read_csv_and_merge_nodes(csv_file_path)
