from flask import Blueprint, jsonify, request
import requests
import csv
import os
import logging
import json

bp = Blueprint('get_sponsored', __name__)

# Function to retrieve bioguideIds from cache.json file
def get_bioguideIds_from_cache():
    CACHE_PATH = os.getenv("CACHE_PATH")
    if not CACHE_PATH:
        logging.error("CACHE_PATH not found in .env file")
        return None

    if not os.path.exists(CACHE_PATH):
        logging.error("Cache file not found at specified path")
        return None

    try:
        with open(CACHE_PATH, 'r') as file:
            cache_data = json.load(file)
        bioguideIds = cache_data.get("bioguideIds", [])
        if not bioguideIds:
            logging.error("bioguideIds not found in cache.json")
            return None
        return bioguideIds
    except Exception as e:
        logging.error(f"Failed to read cache file: {str(e)}")
        return None

# Function to flatten nested dictionaries and lists
def flatten_dict(d, parent_key='', sep='_'):
    items = []
    for k, v in d.items():
        new_key = f"{parent_key}{sep}{k}" if parent_key else k
        if isinstance(v, dict):
            items.extend(flatten_dict(v, new_key, sep=sep).items())
        elif isinstance(v, list):
            for i, item in enumerate(v):
                items.append((f"{new_key}_{i}", flatten_dict(item, '', sep=sep)))
        else:
            items.append((new_key, v if v is not None and v != "" else "NONE"))
    return dict(items)

# Function to write data to CSV
def write_to_csv(data, filename):
    keys = set()
    for item in data:
        keys.update(item.keys())

    with open(filename, 'w', newline='') as output_file:
        dict_writer = csv.DictWriter(output_file, fieldnames=keys)
        dict_writer.writeheader()
        dict_writer.writerows(data)

@bp.route('/get_sponsored', methods=['GET'])
def get_sponsored_legislation():
    try:
        # Retrieve bioguideIds from cache.json
        bioguideIds = get_bioguideIds_from_cache()
        if not bioguideIds:
            return jsonify({"error": "bioguideIds not found"}), 404

        all_data = []

        for bioguideId in bioguideIds:
            # Make request to Congress API
            api_key = os.getenv("CONGRESS_API_KEY")
            url = f"https://api.congress.gov/v3/member/{bioguideId}/sponsored-legislation?api_key={api_key}"
            response = requests.get(url)

            if response.status_code != 200:
                logging.error(f"Failed to retrieve sponsored legislation for bioguideId {bioguideId}: {response.text}")
                return jsonify({"error": f"Failed to retrieve data from Congress API for bioguideId {bioguideId}"}), 500

            data = response.json().get("sponsoredLegislation", [])

            # Add sponsored_by column and handle nested items
            for item in data:
                flattened_item = flatten_dict(item)
                flattened_item["sponsored_by"] = bioguideId
                if not any(flattened_item.values()):
                    continue  # Skip empty rows
                all_data.append(flattened_item)

        if not all_data:
            return jsonify({"error": "No sponsored legislation found for the given bioguideIds"}), 404

        # Write data to CSV
        csv_filename = f"sponsored_legislation.csv"
        write_to_csv(all_data, csv_filename)

        return jsonify({"message": "Data written to CSV successfully", "filename": csv_filename})

    except Exception as e:
        logging.error(f"An error occurred: {str(e)}")
        return jsonify({"error": str(e)}), 500
