use std::time::Duration;

use orama_js_pool::{DomainPermission, ExecOptions, Pool, RuntimeError};

// This code is generated by building `open_ai_js_example` code
// and replacing `./openai.js` content with the `open_ai_js_example/dist/openai.js` one
static CODE: &str = include_str!("./openai.js");

#[tokio::main]
async fn main() -> Result<(), RuntimeError> {
    let open_api_key =
        std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY environment variable is not set");

    let pool = Pool::builder()
        .max_size(10) // 10 workers in the pool
        .with_evaluation_timeout(Duration::from_millis(200))
        .with_domain_permission(DomainPermission::AllowAll) // Allow all domains for OpenAI API
        .add_module("openai", CODE.to_string())
        .build()
        .await?;

    let params = vec![
        open_api_key,
        "gpt-4o".to_string(),
        "Say this is a test".to_string(),
    ];

    let result: String = pool
        .exec(
            "openai",      // module name
            "getResponse", // function name
            &params,
            ExecOptions::new().with_timeout(Duration::from_secs(5)),
        )
        .await?;

    // Output:
    // ```
    // This is a test.
    // ```
    println!("{result}");

    Ok(())
}
