use std::fs;
use std::path::Path;

pub fn download_file(url: &str, dest_path: &str) {
    println!("Downloading {dest_path} from {url}");
    let dest_path = Path::new(dest_path);
    let response = reqwest::blocking::get(url)
        .expect("Failed to send HTTP request")
        .error_for_status()
        .expect("Received error response from server");

    let content = response.bytes().expect("Failed to read response body");

    fs::write(dest_path, &content).expect("Failed to write file to destination");
}

fn main() {
    download_file(
        "https://unpkg.com/htmx.org@2.0.4/dist/htmx.min.js",
        "src/htmx.min.js",
    );

    download_file(
        "https://cdn.jsdelivr.net/npm/flowbite@2.5.2/dist/flowbite.min.css",
        "src/flowbite.min.css",
    );
    download_file(
        "https://cdn.jsdelivr.net/npm/flowbite@2.5.2/dist/flowbite.min.js",
        "src/flowbite.min.js",
    );

    download_file(
        "https://fonts.gstatic.com/s/materialsymbolsoutlined/v226/kJF1BvYX7BgnkSrUwT8OhrdQw4oELdPIeeII9v6oDMzByHX9rA6RzaxHMPdY43zj-jCxv3fzvRNU22ZXGJpEpjC_1v-p_4MrImHCIJIZrDCvHOej.woff2",
        "src/material.woff2",
    );

    println!("cargo:rerun-if-changed=build.rs");
}