#include <iostream>
#include "adsb-helpers.hpp"

int N_THREADS = 4;

std::vector<std::string> scan_directory_for_json(const std::string& directory_path) {
    std::vector<std::string> json_files;
    for (const auto& entry : std::filesystem::directory_iterator(directory_path)) {
        if (entry.path().extension() == ".json" || entry.path().extension() == ".json.gz") {
            json_files.push_back(entry.path().string());
        }
        if (entry.is_directory()) {
            auto nested_files = scan_directory_for_json(entry.path().string());
            json_files.insert(json_files.end(), nested_files.begin(), nested_files.end());
        }
    }
    return json_files;
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <input_adsb_directory>" << std::endl;
        return 1;
    }
    std::string input_directory = argv[1];

    // Get list of all .json files in the input directory
    std::vector<std::string> json_files = scan_directory_for_json(input_directory);
    std::cout << "Found " << json_files.size() << " JSON files." << std::endl;

    // OpenMP for parallel processing
    #pragma omp parallel for num_threads(N_THREADS)
    for (size_t i = 0; i < json_files.size(); ++i) {
        const std::string& input_filepath = json_files[i];
        std::string output_filepath = input_filepath + ".csv"; // Example output path
        try {
            process_adsb_file(input_filepath, output_filepath);
        } catch (const std::exception& e) {
            std::cerr << "Error processing file " << input_filepath << ": " << e.what() << std::endl;
        }
    }
    return 0;
}