use dagger_rust::build::{RustVersion, SlimImage};

#[tokio::main]
pub async fn main() -> eyre::Result<()> {
    dagger_sdk::connect(|client| async move {
        let rust_build = dagger_rust::leptos::LeptosBuild::new(client.clone());

        let containers = rust_build
            .build_release(
                Some("testdata"),
                RustVersion::Nightly,
                &["crates/*"],
                &[
                    "openssl",
                    "libssl-dev",
                    "pkg-config",
                    "musl-tools",
                    "ca-certificates",
                ],
                vec![SlimImage::Debian {
                    image: "debian:bullseye".into(),
                    deps: vec![
                        "openssl".into(),
                        "libssl-dev".into(),
                        "pkg-config".into(),
                        "musl-tools".into(),
                        "ca-certificates".into(),
                    ],
                    architecture: dagger_rust::build::BuildArchitecture::Amd64,
                }],
                "hackernews_axum",
            )
            .await?;

        let container = containers.first().unwrap();

        container.directory("/mnt/app").export("output").await?;

        let tunnel = client.host().tunnel(
            container
                .with_env_variable("LEPTOS_SITE_ADDR", "0.0.0.0:8080")
                .with_exec(vec!["/mnt/app/hackernews_axum"])
                .as_service(),
        );

        tunnel.start().await?;

        let endpoint = tunnel
            .endpoint_opts(
                dagger_sdk::ServiceEndpointOptsBuilder::default()
                    .scheme("http")
                    .build()?,
            )
            .await?;

        println!("running on: {endpoint}, press enter to stop");

        std::io::stdin().read_line(&mut String::new()).unwrap();

        Ok(())
    })
    .await?;
    Ok(())
}
