use std::sync::Arc;

use async_trait::async_trait;
use dagger_sdk::Container;

use crate::dagger_middleware::DaggerMiddleware;

use super::RustService;

pub struct ClapSanityTest {
    bin_name: String,
}

impl ClapSanityTest {
    pub fn new(bin_name: impl Into<String>) -> Self {
        Self {
            bin_name: bin_name.into(),
        }
    }
}

#[async_trait]
impl DaggerMiddleware for ClapSanityTest {
    async fn handle(&self, container: Container) -> eyre::Result<Container> {
        Ok(container.with_exec(vec![&self.bin_name, "--help"]))
    }
}

pub trait ClapSanityTestExt {
    fn with_clap_sanity_test(&mut self) -> &mut Self {
        self
    }
}

impl ClapSanityTestExt for RustService {
    fn with_clap_sanity_test(&mut self) -> &mut Self {
        self.with_stage(super::RustServiceStage::AfterPackage(Arc::new(
            ClapSanityTest::new(&self.bin_name),
        )));

        self
    }
}
