use std::time::Duration;

use alfrusco::{config, URLItem, Workflow};
use clap::Parser;

#[derive(Parser)]
struct URLItemsWorkflow {}

pub fn main() {
    env_logger::init();
    let command = URLItemsWorkflow {};
    alfrusco::execute(&config::AlfredEnvProvider, command, &mut std::io::stdout());
}

impl alfrusco::Runnable for URLItemsWorkflow {
    type Error = alfrusco::Error;
    fn run(self, wf: &mut Workflow) -> Result<(), Self::Error> {
        wf.skip_knowledge(true);
        wf.cache(Duration::from_mins(1), true);
        wf.append_items(vec![
            URLItem::new("DuckDuckGo", "https://www.duckduckgo.com").into(),
            URLItem::new("Google", "https://www.google.com").into(),
        ]);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_url_items_workflow() {
        let command = URLItemsWorkflow {};
        let dir = tempfile::tempdir().unwrap().keep();
        let mut buffer = Vec::new();
        alfrusco::execute(&config::TestingProvider(dir), command, &mut buffer);
        let output = String::from_utf8(buffer).unwrap();
        println!("URL items: {}", output);
        assert!(output.contains("\"title\":\"DuckDuckGo\""));
        assert!(output.contains("\"cache\":{\"seconds\":60,\"loosereload\":true}"));
    }
}
