from flask import Blueprint, render_template, request
import json

bp = Blueprint('person', __name__)

# Load person data from JSON file
with open('../person_data.json') as f:
    person_data = json.load(f)

@bp.route('/', methods=['GET', 'POST'])
def index():
    persons = get_persons()
    relationships = ['SPONSORED', 'COSPONSORED']

    if request.method == 'POST':
        relationship_type = request.form.get('relationship_type')

        # Fetch related nodes based on selected relationship
        with driver.session() as session:
            if relationship_type == 'SPONSORED':
                result = session.run("MATCH (p:Person)-[:SPONSORED]->(l:Legislation) RETURN p.name AS person")
            elif relationship_type == 'COSPONSORED':
                result = session.run("MATCH (p:Person)-[:COSPONSORED]->(l:Legislation) RETURN p.name AS person")
            else:
                # Fetch all unique persons
                result_persons = session.run("MATCH (p:Person) RETURN DISTINCT p.name AS name")
                persons = [record['name'] for record in result_persons]

        if relationship_type:
            persons = list(set(record['person'] for record in result))

    return render_template('graph.html', persons=persons, relationships=relationships)
