use std::string::String;
use std::error::Error;
use fancy_regex::Regex;
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator};
use rayon::iter::ParallelIterator;
use regexle::utils::{read_csv, read_lines, write_to_csv};
use regexle::word::{Answer, Word};


fn map_to_answers(regex: String, words: &Vec<Word>) -> Vec<Answer> {
    let re = Regex::new(&*regex).unwrap();
    let answers: Vec<Answer> = words.par_iter()
        .filter(|w| re.is_match(&*w.word).unwrap_or(false))
        .map(|word| Answer {
            count: word.count,
            question: regex.clone(),
            answer: word.word.clone(),
        })
        .collect();
    answers
}

fn create_answers(regexes: Vec<String>, words: &Vec<Word>) -> Result<Vec<Answer>, Box<dyn Error>> {
    let answers = regexes.par_iter()
        .map(|regex| { map_to_answers(regex.to_string(), words) })
        .filter(|vec| vec.len() > 0)
        .flatten()
        .collect();
    Ok(answers)
}


fn main() -> Result<(), Box<dyn Error>> {
    println!("Hello, world!");


    let regex_chart = "./regex.csv";
    let regexes = read_lines(regex_chart)?;

    let word_freq_chart = "./word_freq.csv";
    let mut words: Vec<Word> = read_csv(word_freq_chart)?;
    words.truncate(10000);
    let processed_words: Vec<Word> = words.into_par_iter().filter(|word| word.word.len() > 2).collect();

    let answers = create_answers(regexes, &processed_words)?;
    println!("Answers: {:?}", answers);

    write_to_csv(answers, "processed_answers.csv")?;
    Ok(())
}
