use crate::error::Error;

use super::types::RecipeGetResponse;

/// Create IMS job ref --> https://csm12-apidocs.svc.cscs.ch/paas/ims/operation/post_v3_job/
pub async fn get(
  shasta_token: &str,
  shasta_base_url: &str,
  shasta_root_cert: &[u8],
  socks5_proxy: Option<&str>,
  recipe_id_opt: Option<&str>,
) -> Result<Vec<RecipeGetResponse>, Error> {
  let client_builder = reqwest::Client::builder()
    .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);

  let client = match socks5_proxy {
    Some(proxy) => client_builder.proxy(reqwest::Proxy::all(proxy)?).build()?,
    None => client_builder.build()?,
  };

  let api_url = if let Some(recipe_id) = recipe_id_opt {
    shasta_base_url.to_owned() + "/ims/v2/recipes" + recipe_id
  } else {
    shasta_base_url.to_owned() + "/ims/v2/recipes"
  };

  let response = client
    .get(api_url)
    .bearer_auth(shasta_token)
    .send()
    .await?
    .error_for_status()?
    .json::<Vec<RecipeGetResponse>>()
    .await?;

  Ok(response)
}
