from flask import Blueprint, jsonify, request
import os
import csv
from neo4j import GraphDatabase
import logging

bp = Blueprint('process_sponsored', __name__)

# Custom logger for the process_sponsored blueprint
process_sponsored_logger = logging.getLogger('ProcessSponsoredLogger')
process_sponsored_logger.setLevel(logging.INFO)
process_sponsored_handler = logging.StreamHandler()
process_sponsored_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
process_sponsored_handler.setFormatter(process_sponsored_formatter)
process_sponsored_logger.addHandler(process_sponsored_handler)

@bp.route('/process_sponsored', methods=['GET'])
def process_sponsored():
    csv_file_path = os.getenv("SPONSORED_LEGISLATION_CSV")

    if not csv_file_path:
        return jsonify({"error": "SPONSORED_LEGISLATION_CSV environment variable is not set"}), 400

    try:
        with open(csv_file_path, mode='r', newline='', encoding='utf-8') as file:
            reader = csv.DictReader(file)
            driver = GraphDatabase.driver(os.getenv("NEO4J_URI"), auth=(os.getenv("NEO4J_USER"), os.getenv("NEO4J_PASSWORD")))
            session = driver.session()

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

                # Log the CSV row
                process_sponsored_logger.info(f"Processing row: {properties}")

                query = (
                    "MERGE (l:legislation {"
                    + ", ".join(f"{key}: $props.{key}" for key in properties)
                    + "})"
                )

                # Log the MERGE query
                process_sponsored_logger.info(f"Executing query: {query}")

                session.run(query, props=properties)

            session.close()
            driver.close()

        return jsonify({"message": f"Processed {reader.line_num - 1} sponsored legislations"}), 200

    except Exception as e:
        process_sponsored_logger.error(f"Error processing sponsored legislation: {e}")
        return jsonify({"error": str(e)}), 500
