use std::{fs::File, io::Cursor};

type FetchResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

#[allow(dead_code)]
async fn fetch_url(url: String, file_name: String) -> FetchResult<()> {
    let response = reqwest::get(url).await?;
    let mut file = File::create(file_name)?;
    let mut content = Cursor::new(response.bytes().await?);
    std::io::copy(&mut content, &mut file)?;
    Ok(())
}

// use reqwest::{header::CONTENT_LENGTH, Client};
// use std::{path::Path, io::Write};

// #[allow(dead_code)]
// async fn download_file_with_progress(url: &str, file_path: &Path) -> Result<(), reqwest::Error> {
//     let client = Client::new();

//     let response = client.head(url).send().await?;
//     let content_length = response.headers()
//         .get(CONTENT_LENGTH)
//         .and_then(|v| v.to_str().ok()).and_then(|v| v.parse::<u64>().ok());

//     if let Some(content_length) = content_length {
//         println!("Downloading {} bytes from {}", content_length, url);
//     } else {
//         println!("Downloading from {}", url);
//     }

//     let mut response = client.get(url).send().await?;

//     let mut file = File::create(file_path)?;

//     let mut bytes_written = 0;
//     while let Some(chunk) = response.chunk().await? {
//         file.write_all(&chunk)?;
//         bytes_written += chunk.len();

//         if let Some(content_length) = content_length {
//             let progress = (bytes_written as f64 / content_length as f64) * 100.0;
//             print!("\rDownloading {:.2}% ({} of {} bytes)", progress, bytes_written, content_length);
//         }
//     }

//     if let Some(content_length) = content_length {
//         println!("\rDownload complete ({} bytes)", content_length);
//     } else {
//         println!("\rDownload complete");
//     }

//     Ok(())
// }
