#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include "acoustic_analyzer/core/feature_extractor.h"
#include "acoustic_analyzer/io/wav_file_source.h"

using namespace acoustic;

int main(int argc, char** argv) {
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <wav_file> [output_bin]" << std::endl;
        return 1;
    }

    WavFileSource wav(argv[1]);
    if (!wav.open()) {
        std::cerr << "Failed to open WAV" << std::endl;
        return 1;
    }

    std::vector<std::vector<float>> audio;
    wav.read(audio, wav.total_samples());
    if (audio.empty()) {
        std::cerr << "Empty audio" << std::endl;
        return 1;
    }

    FeatureExtractorParams p;
    p.sample_rate = wav.sample_rate();
    FeatureExtractor ext(p);
    std::vector<float> mel;
    if (!ext.extract(audio, mel)) {
        std::cerr << "Extraction failed" << std::endl;
        return 1;
    }

    std::cout << "Extracted Mel shape: " << p.n_mels << " x " << (mel.size() / p.n_mels) << std::endl;
    std::cout << "Min: " << *std::min_element(mel.begin(), mel.end())
              << " Max: " << *std::max_element(mel.begin(), mel.end()) << std::endl;

    if (argc >= 3) {
        std::ofstream out(argv[2], std::ios::binary);
        out.write(reinterpret_cast<const char*>(mel.data()), mel.size() * sizeof(float));
        std::cout << "Saved to " << argv[2] << std::endl;
    }
    return 0;
}
