import glob
from InvestmentManager import investmentmanager_bp
import json
from flask import jsonify
import os

from modules.core import storage_paths

@investmentmanager_bp.route('/leadhunter/get_scraped_offers', methods=['GET'])
def get_scraped_offers():
    data_dir = storage_paths.scrapers_data_dir()
    all_data = []

    for file_path in glob.glob(os.path.join(data_dir, 'olx*_cleaned.json')):
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                content = json.load(f)
                if isinstance(content, list):
                    all_data.extend(content)
        except Exception as e:
            print(f"Błąd przy wczytywaniu pliku {file_path}: {e}")

    return jsonify(all_data), 200